我正在使用 Flutter 开发一个移动应用程序,有一个函数需要一个 Flask API 来将图像从 Flutter 发送到 Flask API 以进行一些处理。
这是我的错误:
E/flutter (14007): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Converting object to an encodable object failed: Instance of '_File'
E/flutter (14007): #0 _JsonStringifier.writeObject (dart:convert/json.dart:687:7)
E/flutter (14007): #1 _JsonStringifier.writeMap (dart:convert/json.dart:768:7)
E/flutter (14007): #2 _JsonStringifier.writeJsonValue (dart:convert/json.dart:723:21)
E/flutter (14007): #3 _JsonStringifier.writeObject (dart:convert/json.dart:678:9)
E/flutter (14007): #4 _JsonStringStringifier.printOn (dart:convert/json.dart:876:17)
E/flutter (14007): #5 _JsonStringStringifier.stringify (dart:convert/json.dart:861:5)
E/flutter (14007): #6 JsonEncoder.convert (dart:convert/json.dart:261:30)
E/flutter (14007): #7 JsonCodec.encode (dart:convert/json.dart:171:45)
E/flutter (14007): #8 ScanState.build.<anonymous closure>.<anonymous closure> (package:mar/sccreens/scanAnalysis.dart:298:78)
E/flutter (14007): #9 ScanState.build.<anonymous closure>.<anonymous closure> (package:mar/sccreens/scanAnalysis.dart:262:36)
E/flutter (14007): #10 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:990:20)
E/flutter (14007): #11 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
E/flutter (14007): #12 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
E/flutter (14007): #13 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
E/flutter (14007): #14 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
E/flutter (14007): #15 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:476:9)
E/flutter (14007): #16 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:93:12)
E/flutter (14007): #17 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:138:9)
E/flutter (14007): #18 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:387:8)
E/flutter (14007): #19 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:136:18)
E/flutter (14007): #20 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:122:7)
E/flutter (14007): #21 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:381:19)
E/flutter (14007): #22 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:361:22)
E/flutter (14007): #23 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:280:11)
E/flutter (14007): #24 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:316:7)
E/flutter (14007): #25 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:280:5)
E/flutter (14007): #26 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:238:7)
E/flutter (14007): #27 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:221:7)
E/flutter (14007): #28 _rootRunUnary (dart:async/zone.dart:1202:13)
E/flutter (14007): #29 _CustomZone.runUnary (dart:async/zone.dart:1097:19)
E/flutter (14007): #30 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1002:7)
E/flutter (14007): #31 _invoke1 (dart:ui/hooks.dart:176:10)
E/flutter (14007): #32 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:276:7)
E/flutter (14007): #33 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter (14007):
这是我在 flutter 中的代码,我认为包含最终响应 = bla bla bla ....:
onPressed: () async{
if (_selectedFile == null) {
return showDialog()
}
else {
final url = 'http://127.0.0.1:5000/image';
//sending a post request to the url
final response = await http.post(url, body: json.encode({'image' : _selectedFile}));