【问题标题】:Flutter Unhandled Exception: DioError [DioErrorType.other]Flutter 未处理异常:DioError [DioErrorType.other]
【发布时间】:2023-03-13 17:00:01
【问题描述】:

我是新来的 api 调用 Flutter。我遇到了麻烦。但实际问题是什么,解决方案是什么?我无法理解!我正在使用 Dio 进行休息 api 调用,并希望在颤动的列表视图中显示它。在我的代码中,我返回类似-

返回 response.data["data"]["data"] .map((json) => ListAlbum.fromJson(json)) .toList();

所以,如果 json 响应不附带查询,则此代码可以正常工作!

模拟器屏幕

Json 输出

{
    "success": true,
    "data": {
        "count": 2,
        "data": [
            {
                "idCertificateRequest": 18,
                "certificateType": "NOC Certificate",
                "publicationStatus": "activated",
                "requestStatus": "Approved",
            }
        ],
        "query": [
            {
                "query": "select SQL_CALC_FOUND_ROWS por_certificate_request.id_certificate_request AS idCertificateRequest, `por_certificate_request`.`certificate_type` as `certificateType`, `por_certificate_request`.`publication_status` as `publicationStatus`, `por_certificate_request`.`request_status` as `requestStatus`, `por_certificate_request`.`requested_by` as `requestedId`, `hr_employee`.`full_name` as `fullName`, `hr_employee`.`employee_custom_id` as `customID`, `por_certificate_request`.`date_created` as `submissionDate`, `supervisorEmp`.`full_name` as `supervisor`, `localHr`.`full_name` as `localHr`, `hr`.`full_name` as `hr`, (CASE WHEN hr_employee.avatar THEN hr_employee.avatar ELSE 'default.jpg' END) as avatar from `por_certificate_request` inner join ((select `id_certificate_request` from `por_certificate_request` where `requested_by` = ? and `request_status` in (?, ?, ?, ?, ?) and `por_certificate_request`.`publication_status` = ?) union all (select `id_certificate_request` from `hr_organization_setup` inner join `por_certificate_request` on `por_certificate_request`.`requested_by` = `hr_organization_setup`.`employee_id` where `hr_organization_setup`.`line_supervisor_id` = ? and `hr_organization_setup`.`publication_status` = ? and `hr_organization_setup`.`working_status` in (?, ?) and `por_certificate_request`.`request_status` = ?) union all (select `id_certificate_request` from `por_employee_hr` inner join `por_certificate_request` on `por_certificate_request`.`requested_by` = `por_employee_hr`.`employee_id` inner join `por_employee_hr_details` on `por_employee_hr_details`.`id_employee_hr` = `por_employee_hr`.`id_employee_hr` where `por_employee_hr_details`.`hr_id` = ? and `por_employee_hr_details`.`status` = ? and `por_employee_hr_details`.`type` = ? and `por_employee_hr_details`.`publication_status` = ? and `por_certificate_request`.`request_status` = ? and `por_certificate_request`.`publication_status` = ?) union all (select `id_certificate_request` from `por_employee_hr` inner join `por_certificate_request` on `por_certificate_request`.`requested_by` = `por_employee_hr`.`employee_id` inner join `por_employee_hr_details` on `por_employee_hr_details`.`id_employee_hr` = `por_employee_hr`.`id_employee_hr` where `por_employee_hr_details`.`hr_id` = ? and `por_employee_hr_details`.`status` = ? and `por_employee_hr_details`.`type` = ? and `por_employee_hr_details`.`publication_status` = ? and `por_certificate_request`.`request_status` = ?)) as `certificates` on `certificates`.`id_certificate_request` = `por_certificate_request`.`id_certificate_request` inner join `hr_employee` on `hr_employee`.`employee_id` = `por_certificate_request`.`requested_by` inner join `hr_organization_setup` on `hr_organization_setup`.`employee_id` = `hr_employee`.`employee_id` inner join `hr_employee` as `supervisorEmp` on `supervisorEmp`.`employee_id` = `hr_organization_setup`.`line_supervisor_id` inner join `por_employee_hr` on `por_employee_hr`.`employee_id` = `por_certificate_request`.`requested_by` inner join `por_employee_hr_details` as `localHrDetails` on `localHrDetails`.`id_employee_hr` = `por_employee_hr`.`id_employee_hr` and `localHrDetails`.`publication_status` = ? and `localHrDetails`.`status` = ? and `localHrDetails`.`type` = ? inner join `hr_employee` as `localHr` on `localHr`.`employee_id` = `localHrDetails`.`hr_id` inner join `por_employee_hr_details` as `hrDetails` on `hrDetails`.`id_employee_hr` = `por_employee_hr`.`id_employee_hr` and `hrDetails`.`publication_status` = ? and `hrDetails`.`status` = ? and `hrDetails`.`type` = ? inner join `hr_employee` as `hr` on `hr`.`employee_id` = `hrDetails`.`hr_id` where `hr_employee`.`publication_status` = ? and `supervisorEmp`.`publication_status` = ? and `hr_organization_setup`.`publication_status` = ? and `hr_organization_setup`.`working_status` in (?, ?) order by `idCertificateRequest` desc limit 20 offset 0",
                "bindings": [
                    2900,
                    "Pending",
                    "Approved",
                    "Denied",
                    "Acknowledged",
                    "Done",
                    "activated",
                    2900,
                    "activated",
                    "Working",
                    "JV",
                    "Pending",
                    2900,
                    "Approved",
                    "business unit",
                    "activated",
                    "Approved",
                    "activated",
                    2900,
                    "Approved",
                    "certificate",
                    "activated",
                    "Acknowledged",
                    "activated",
                    "Approved",
                    "business unit",
                    "activated",
                    "Approved",
                    "certificate",
                    "activated",
                    "activated",
                    "activated",
                    "Working",
                    "JV"
                ],
                "time": 5.85
            }
        ]
    },
    "employeeId": 2900
}

使用 Dio 调用 Api

Future<List<ListAlbum>> listData() async {
  final token = 'my_api_token';
  String url = 'https://portal-api.jomakhata.com/api/requestedCertificateList?token=${token}';
  print('url');
  Dio dio = new Dio();
  dio.options.headers['Content-Type'] = 'application/json';
  print(dio.options.headers);
  final body = {'limit': 1, 'orderBy': 'idCertificateRequest', 'orderType': 'DESC'};
  print(body);
  final response = await dio.post(url, data: body);
  print(response);

  print('ok!');

  if (response.statusCode == 200) {

    print(response.statusCode);
    return response.data["data"]["data"]
        .map<ListAlbum>((json) => ListAlbum.fromJson(json))
        .toList();
  } else {
    throw Exception('Failed!');
  }
}

这是错误!

Restarted application in 1,249ms.
I/flutter ( 5188): {limit: 1, orderBy: idCertificateRequest, orderType: DESC}
E/flutter ( 5188): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: DioError [DioErrorType.other]: FormatException: Unexpected character (at character 4710)
E/flutter ( 5188): ..."],"time":6.87}]},"employeeId":2900}{"message":"SQLSTATE[22001]: String ...
E/flutter ( 5188):                                        ^
E/flutter ( 5188): 
E/flutter ( 5188): #0      DioMixin.assureDioError (package:dio/src/dio_mixin.dart:821:20)
E/flutter ( 5188): #1      DioMixin._dispatchRequest (package:dio/src/dio_mixin.dart:678:13)
E/flutter ( 5188): <asynchronous suspension>
E/flutter ( 5188): #2      DioMixin.fetch.<anonymous closure>.<anonymous closure> (package:dio/src/dio_mixin.dart)
E/flutter ( 5188): <asynchronous suspension>

【问题讨论】:

    标签: flutter listview


    【解决方案1】:

    我试过这个,当 json 只包含 json 格式的数据时它工作得很好。看,在 josn 响应中,一些键值对。比如,“success”:true,“data”:{},但是如果我们只获取 json 数据,它就可以正常工作。

        {
            "success": true,
            "data": {
                "count": 2,
                "data": [
                    {
                        "idCertificateRequest": 18,
                        "certificateType": "NOC Certificate",
                    },
                   
                ], //it works if josn formet like this
             }
       }
    

    但如果 json 响应带有查询,例如 "query":[{ "query": "select SQL_CALC_ ..."}] 那么它会在 android 中显示错误。但它为什么会发生不完全知道。但我认为可能是 json 解析或 json 编码类型问题。

    {
        "success": true,
        "data": {
            "count": 2,
            "data": [
                {
                    "idCertificateRequest": 18,
                    "certificateType": "NOC Certificate",
                },
            ], //it dosen't work if josn formet like this. i mean when comes  query key and value pair, it shows an error. even it can't print anythings..
            "query": [
                {
                    "query": "select SQL_CALC_FOUND_ROWS por_certificate_request.id_certificate_request AS idCertificateRequest, 
                }
    }
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 好的!我将编辑我的答案!
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 2023-04-03
    • 2022-01-23
    • 2021-03-06
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多