未处理的异常:FormatException:输入意外结束(在字符 1 处)

Posted

技术标签:

【中文标题】未处理的异常:FormatException:输入意外结束(在字符 1 处)【英文标题】:Unhandled Exception: FormatException: Unexpected end of input (at character 1) 【发布时间】:2019-12-19 18:48:38 【问题描述】:

我正在尝试在 Flutter 中使用 http 解析以下地图

 Map<String, dynamic> map = 
      "studentID": "$widget.userId".toString(),
      "gstNumber": "$widget.feesBloc?.gstNumberValue" ?? "",
      "entityName": "$widget.feesBloc?.gstEntityValue" ?? "",
      "paymentInfo": 
        "amount": "$tempAmount".toString(),
        "razorpay_payment_id": "$razorPaymentId" ?? "",
        "usedWB": "$_walletAmount"
      ,
      "feeInfo": 
        "feeID": "$widget.feesData.feeID".toString(),
        "type": "$widget.feesData.type",
        "category": "$widget.feesData.category".toString(),
        "feeCode": "$widget.feesData.feeCode".toString(),
        "feeShortName": "$widget.feesData.feeShortName" ?? "",
        "basicAMT": "$widget.feesData.basicAMT".toString(),
        "dueDate": "$widget.feesData.dueDate".toString(),
        "grandAMT": "$widget.feesData.grandAMT",
        "status": "$widget.feesData.status".toString(),
        "collectionID": "$widget.feesData.collectionID".toString(),
        "dateStatus": "$widget.feesData.dateStatus".toString(),
        "collectionDetailStatus": "-1",
        "schemeInfo": 
          "schemeID": "1",
          "schemeNo": "$widget.feesData.schemeInfo.schemeNo",
          "schemeTxt": "a" ?? "",
          "schemeStatus": "$widget.feesData.schemeInfo.schemeStatus"
        ,
        "lateFeeInfo": 
          "lateDays": "$widget.feesData.lateFeeInfo.lateDays",
          "lateFeePerDay":
              "$widget.feesData.lateFeeInfo.lateFeePerDay".toString(),
          "lateFeeAMT": "$widget.feesData.lateFeeInfo.lateFeeAMT"
        ,
        "taxInfo": 
          "tax": "$widget.feesData.taxInfo.tax".toString(),
          "taxAMT": "$widget.feesData.taxInfo.taxAMT".toString(),
          "taxAddedAMT": "$widget.feesData.taxInfo.taxAddedAMT"
        ,
        "scholarshipInfo": 
          "scholarship":
              "$widget.feesData.scholarshipInfo.scholarship".toString(),
          "scholarshipAMT": "$widget.feesData.scholarshipInfo.scholarshipAMT"
        
      

下面是我的api代码

Map<String, String> headers = 
      'Content-type': 'application/json',
      'Accept': 'application/json',
    ;

    final response = await http.post(FEE_PAYMENT_URL,
        headers: headers, body: json.encode(jsonMap));

我的状态码为 200,但 response.body 没有返回任何内容,因此解码失败

以下是完整的错误

Unhandled Exception: FormatException: Unexpected end of input (at character 1)
E/flutter ( 8915): 
E/flutter ( 8915): ^
E/flutter ( 8915): 
E/flutter ( 8915): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1392:5)
E/flutter ( 8915): #1      _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:510:7)
E/flutter ( 8915): #2      _parseJson (dart:convert-patch/convert_patch.dart:30:10)
E/flutter ( 8915): #3      JsonDecoder.convert (dart:convert/json.dart:493:36)
E/flutter ( 8915): #4      JsonCodec.decode (dart:convert/json.dart:151:41)
E/flutter ( 8915): #5      FeePaymentApiProvider.submitFeePayment (package:dice/resources/fee_payment/fee_payment_api_provider.dart:22:42)
E/flutter ( 8915): <asynchronous suspension>
E/flutter ( 8915): #6      FeePaymentRepository.submitFeePayment (package:dice/resources/fee_payment/fee_payment_repository.dart:14:48)
E/flutter ( 8915): <asynchronous suspension>
E/flutter ( 8915): #7      FeesBloc.submitFeePayment (package:dice/bloc/fees_bloc/fees_bloc.dart:54:40)
E/flutter ( 8915): <asynchronous suspension>
E/flutter ( 8915): #8      _PresentFeesListItemState._submitDataToServerAfterPaymentIsSuccessful (package:dice/screens/fees/present_fees/present_fees_list_item.dart:517:21)
E/flutter ( 8915): #9      _PresentFeesListItemState.calculateFees (package:dice/screens/fees/present_fees/present_fees_list_item.dart:202:9)
E/flutter ( 8915): #10     _PresentFeesListItemState.build.<anonymous closure>.<anonymous closure> (package:dice/screens/fees/present_fees/present_fees_list_item.dart:165:21)
E/flutter ( 8915): #11     _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 8915): #12     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 8915): #13     _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 8915): #14     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 8915): #15     Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 8915): #16     Future._complete (dart:async/future_impl.dart:473:7)
E/flutter ( 8915): #17     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 8915): #18     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:28:18)
E/flutter ( 8915): #19     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:294:13)
E/flutter ( 8915): #20     checkInternetConnection (package:dice/helpers/check_internet_connection.dart)
E/flutter ( 8915): <asynchronous suspension>
E/flutter ( 8915): #21     _PresentFeesListItemState.build.<anonymous closure> (package:dice/screens/fees/present_fees/present_fees_list_item.dart:163:17)
E/flutter ( 8915): #22     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14)
E/flutter ( 8915): #23     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:711:32)
E/flutter ( 8915): #24     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 8915): #25     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter ( 8915): #26     TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:312:7)
E/flutter ( 8915): #27     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 8915): #28     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter ( 8915): #29     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 8915): #30     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 8915): #31     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 8915): #32     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 8915): #33     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 8915): #34     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 8915): #35     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 8915): #36     _invoke1 (dart:ui/hooks.dart:250:10)
E/flutter ( 8915): #37     _dispatchPointerDataPacket (dart:ui/hooks.dart:159:5)
E/flutter ( 8915): 

任何帮助将不胜感激。我在堆栈上完成了以下问题,但它们没有用。我正在将复杂的地图结构以正确的方式传递给我的 dart 中的 api,该 api 在邮递员上运行良好

【问题讨论】:

我们如何帮助您解决这个问题?您基本上是在问我们为什么响应正文为空,但我们对您的 FEE_PAYMENT_URL API 一无所知。如果响应代码为 200,则表示一切正常。您对响应正文有什么期望? 你能用邮递员getpostman.com测试你的api并粘贴你得到的JSON字符串吗? @chunhunghan 我刚刚收到一条消息,以 json 格式显示成功 【参考方案1】:

错误应该指出正在尝试解析的意外格式FormatException: Unexpected end of input (at character 1)。从您共享的日志中,它似乎突出显示了一个空值。您正在解析的 json 很可能是空的。您可以在解析之前添加检查器以避免此问题。

if(response.body.isNotEmpty)
  // Do something
  json.decode(response.body);

【讨论】:

以上是关于未处理的异常:FormatException:输入意外结束(在字符 1 处)的主要内容,如果未能解决你的问题,请参考以下文章

DatePicker System.FormatException

System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小

“System.FormatException”类型的未经处理的异常在 System.IdentityModel.dll 中发生 其他信息: 十六进制字符串格式无效。

输入字符串的格式不正确 #2

输入字符串的格式不正确,格式为 1 和 2

aspose 单元格工作簿引发了“System.FormatException”类型的异常