0

这是我使用邮递员向我的节点 API 发出的 Post 请求的屏幕截图

我正在开发 Flutter 应用程序来注册用户,并且我正在尝试将使用 Flutter image_picker 插件从我的 Flutter 应用程序中挑选出来的图像文件发布到我的节点 API。我从一个屏幕中的图库中挑选了图像并将其发送到另一个屏幕。

    File imageFile;

    _openGallery(BuildContext context) async{

    var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
    if(picture != null){
    File croppedFile = await ImageCropper.cropImage(
    sourcePath: picture.path,
    aspectRatioPresets: Platform.isAndroid
    ?[
              CropAspectRatioPreset.square,
              CropAspectRatioPreset.ratio3x2,
              CropAspectRatioPreset.original,
              CropAspectRatioPreset.ratio4x3,
              CropAspectRatioPreset.ratio16x9
    ]
    :[
      CropAspectRatioPreset.original,
              CropAspectRatioPreset.square,
              CropAspectRatioPreset.ratio3x2,
              CropAspectRatioPreset.ratio4x3,
              CropAspectRatioPreset.ratio5x3,
              CropAspectRatioPreset.ratio5x4,
              CropAspectRatioPreset.ratio7x5,
              CropAspectRatioPreset.ratio16x9
    ],

    androidUiSettings: AndroidUiSettings(
         toolbarTitle: 'Crop Your photo',
          toolbarColor: Color(0xFFFF9006),
          toolbarWidgetColor: Colors.white,
          initAspectRatio: CropAspectRatioPreset.original,
          lockAspectRatio: false),
    );

    this.setState((){
          //  imageFile = croppedFile;
           imageFile = croppedFile;

    });
    Navigator.of(context).pop();
    _showImg();
}

我已经使用材料页面路由器将图像文件传递到下一个屏幕

Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => SetupStepTwo(imageFile: imageFile)));

这是我与此图像相关的下一页代码

    final File imageFile;

    SetupStepTwo(
      {Key key,this.imageFile,}) : super(key: key);

                      onTap{

                         if (otpcheckNull()) {

                            File _file = widget.imageFile;

                            final body = {
                              "name": widget.otpName,
                              "gender": widget.gender,
                              "bday": widget.bday,
                              "email": widget.email,
                              "phone": widget.phone,
                              "profileImage": _file,
                            };

                            RegisterUserService.RegisterUser(body)
                                .then((success) {
                              if (success) {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (context) => Homepage()));
                              }
                            });
                          } 
                           else {
                               errorCheckDialog();
                               }

   }

这是我的 Api 调用实现代码

class RegisterUserService {
  static Future<bool> RegisterUser(body) async {

    final response =
        await http.post('http://morning-fortress-69187.herokuapp.com/api/user/register', body: jsonEncode(body));

    var data = response.body;
    print(body);
    print(json.decode(data));

    Map<String, dynamic> res_data = jsonDecode(data);
    log(res_data['loginstatus']);
    if (res_data['loginstatus'] == 'olduser') {
      return true;
    } else {
      return false;
    }
    // return false;
  }
}

它给了我这个错误

E/flutter (15655): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Converting object to an encodable object failed: Instance of '_File'
E/flutter (15655): #0      _JsonStringifier.writeObject  (dart:convert/json.dart:649:7)
E/flutter (15655): #1      _JsonStringifier.writeMap  (dart:convert/json.dart:730:7)
E/flutter (15655): #2      _JsonStringifier.writeJsonValue  (dart:convert/json.dart:685:21)
E/flutter (15655): #3      _JsonStringifier.writeObject  (dart:convert/json.dart:640:9)
E/flutter (15655): #4      _JsonStringStringifier.printOn  (dart:convert/json.dart:836:17)
E/flutter (15655): #5      _JsonStringStringifier.stringify  (dart:convert/json.dart:821:5)
E/flutter (15655): #6      JsonEncoder.convert  (dart:convert/json.dart:257:30)
E/flutter (15655): #7      JsonCodec.encode  (dart:convert/json.dart:168:45)
E/flutter (15655): #8      jsonEncode  (dart:convert/json.dart:82:10)
E/flutter (15655): #9      RegisterUserService.RegisterUser 
package:folk/…/ApiServices/RegisterNewUser.dart:15
E/flutter (15655): #10     _GetLocationState.build.<anonymous closure> 
package:folk/…/Login/location.dart:190
E/flutter (15655): #11     _InkResponseState._handleTap 
package:flutter/…/material/ink_well.dart:706
E/flutter (15655): #12     _InkResponseState.build.<anonymous closure> 
package:flutter/…/material/ink_well.dart:789
E/flutter (15655): #13     GestureRecognizer.invokeCallback 
package:flutter/…/gestures/recognizer.dart:182
E/flutter (15655): #14     TapGestureRecognizer.handleTapUp 
package:flutter/…/gestures/tap.dart:486
E/flutter (15655): #15     BaseTapGestureRecognizer._checkUp 
package:flutter/…/gestures/tap.dart:264
E/flutter (15655): #16     BaseTapGestureRecognizer.handlePrimaryPointer 
package:flutter/…/gestures/tap.dart:199
E/flutter (15655): #17     PrimaryPointerGestureRecognizer.handleEvent 
package:flutter/…/gestures/recognizer.dart:467
E/flutter (15655): #18     PointerRouter._dispatch 
package:flutter/…/gestures/pointer_router.dart:76
E/flutter (15655): #19     PointerRouter._dispatchEventToRoutes.<anonymous closure> 
package:flutter/…/gestures/pointer_router.dart:117
E/flutter (15655): #20     _LinkedHashMapMixin.forEach  (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (15655): #21     PointerRouter._dispatchEventToRoutes 
package:flutter/…/gestures/pointer_router.dart:115
E/flutter (15655): #22     PointerRouter.route 
package:flutter/…/gestures/pointer_router.dart:101
E/flutter (15655): #23     GestureBinding.handleEvent 
package:flutter/…/gestures/binding.dart:218
E/flutter (15655): #24     GestureBinding.dispatchEvent 
package:flutter/…/gestures/binding.dart:198
E/flutter (15655): #25     GestureBinding._handlePointerEvent 
package:flutter/…/gestures/binding.dart:156
E/flutter (15655): #26     GestureBinding._flushPointerEventQueue 
package:flutter/…/gestures/binding.dart:102
E/flutter (15655): #27     GestureBinding._handlePointerDataPacket 
package:flutter/…/gestures/binding.dart:86
E/flutter (15655): #28     _rootRunUnary  (dart:async/zone.dart:1138:13)
E/flutter (15655): #29     _CustomZone.runUnary  (dart:async/zone.dart:1031:19)
E/flutter (15655): #30     _CustomZone.runUnaryGuarded  (dart:async/zone.dart:933:7)
E/flutter (15655): #31     _invoke1  (dart:ui/hooks.dart:273:10)
E/flutter (15655): #32     _dispatchPointerDataPacket  (dart:ui/hooks.dart:182:5)

错误截图

我怎么解决这个问题 ?

4

1 回答 1

0

似乎您在“用户/注册”处的 API 端点仅接收主体的 profileImage 参数中的名称。由于文件和图像是二进制数据,我建议在声明正文时这样做:

   final body = {             "name": widget.otpName,
                          "gender": widget.gender,
                          "bday": widget.bday,
                          "email": widget.email,
                          "phone": widget.phone,
                          "profileImage": base64Encode(_file.readAsBytesSync())                          }; 

这样 json.encode 接收一个字符串而不是一个文件,它知道如何编码。而您现在必须解码后端帖子正文中收到的 base 64 字符串。

于 2020-02-05T14:57:57.357 回答