Flutter json.decode 问题 <!doctype html> 错误
Posted
技术标签:
【中文标题】Flutter json.decode 问题 <!doctype html> 错误【英文标题】:Flutter json.decode issue <!doctype html> error 【发布时间】:2020-09-17 20:08:57 【问题描述】:我正在尝试将条纹支付与后端 Laravel 集成到我的 Flutter 项目中。出于某种原因,我在 json.decode 中遇到错误。
E/flutter (10999): [错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:FormatException:意外字符(在 字符 1) E/flutter (10999):
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0)),
color: Colors.redAccent,
onPressed: ()
var payment = Payment();
payment.name = _cardHolderName.text;
payment.email = _cardHolderEmail.text;
payment.cardNumber = _cardNumber.text;
payment.expiryMonth = _expiryMonth.text;
payment.expiryYear = _expiryYear.text;
payment.cvcNumber = _cvcNumber.text;
_makePayment(context, payment);
,
child: Text('Make Payment',
style: TextStyle(color: Colors.white)),
),
void _makePayment(BuildContext context, Payment payment) async
PaymentService _paymentService = PaymentService();
var paymentData = await _paymentService.makePayment(payment);
var result = json.decode(paymentData.body);
if (result['result'] == true)
CartService _cartService = CartService();
this.widget.cartItems.forEach((cartItem)
_cartService.deleteCartItemById(cartItem.productId);
);
_showPaymentSuccessMessage(context);
Timer(Duration(seconds: 2), ()
Navigator.pop(context);
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
);
支付服务
class PaymentService
Repository _repository;
PaymentService()
_repository = Repository();
makePayment(Payment payment) async
return await _repository.httpPost('make-payment', payment.toJson());
Laravel 代码
public function makePayment(Request $request)
\Stripe\Stripe::setApiKey('sk_test_7422pw1lMsdRfN2FaPmfFKlt00AtiIJD9O');
$token = Token::create([
'card'=>[
'number'=>$request-input('cardNumber'),
'exp_month'=>$request-input('expiryMonth'),
'exp_year'=>$request-input('expiryYear'),
'cvc'=>$request-input('cvcNumber')
]
]);
\Stripe\PaymentIntent::create([
'amount' => 1000,
'currency' => 'inr',
'source' => $token,
'receipt_email' => $request->input('email'),
]);
错误
E/flutter (10999): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (10999): <!doctype html>
E/flutter (10999): ^
E/flutter (10999):
E/flutter (10999): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1394:5)
E/flutter (10999): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1261:9)
E/flutter (10999): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:926:22)
E/flutter (10999): #3 _parseJson (dart:convert-patch/convert_patch.dart:31:10)
E/flutter (10999): #4 JsonDecoder.convert (dart:convert/json.dart:495:36)
E/flutter (10999): #5 JsonCodec.decode (dart:convert/json.dart:153:41)
E/flutter (10999): #6 _PaymentScreenState._makePayment (package:factory2homes/screens/payment_screen.dart:160:23)
E/flutter (10999): <asynchronous suspension>
E/flutter (10999): #7 _PaymentScreenState.build.<anonymous closure> (package:factory2homes/screens/payment_screen.dart:142:23)
E/flutter (10999): #8 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (10999): #9 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (10999): #10 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (10999): #11 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (10999): #12 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (10999): #13 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter (10999): #14 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (10999): #15 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter (10999): #16 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (10999): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (10999): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (10999): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (10999): #20 _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (10999): #21 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (10999): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (10999): #23 _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (10999): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (10999):
【问题讨论】:
【参考方案1】:当服务器返回 html
正文或服务器端在处理您的请求时发生错误时,会发生此错误。尝试使用调试器并观察请求的 response
以查看响应 status
是否为 200
并检查响应 body
以查看服务器为您的请求发送的内容。
同时尝试将jsonDecode
包裹在try-catch
中以处理未来的异常。
【讨论】:
以上是关于Flutter json.decode 问题 <!doctype html> 错误的主要内容,如果未能解决你的问题,请参考以下文章