【问题标题】:Flutter Retrofit upload fileFlutter Retrofit 上传文件
【发布时间】:2021-11-29 02:51:15
【问题描述】:

我正在使用颤振改造包,正在尝试将数据上传到服务器(Laravel)。基本上,该文件应作为 MultiPart 部分注释发送。另外,我应该与文件(在我的情况下是一张图片)一起发送不同的值,所以我使用了@Part() map

这是我的代码

@POST('register')
  Future<RegisterResponse> register(@Body() Map<String, dynamic> body, @Part() File pic);

这是我的要求

Map<String, dynamic> body = {
                            "name":" widget.fullName",
                            "username": "widget.userName",
                            "phone": "698281556",
                            "country_code": "+213",
                            "email": "widget.email@gmail.com",
                            "birth_date": "18-09-2021",
                            "pic": imageFile,
                            "lat": 36.347285,
                            "lon": 6.603381,
                            "address": "widget.userName",
                            "city": "widget.userName",
                            "residential": "widget.userName",
                            "device_token": "widget.userName",
                            "code": "123456",
                            "tags": [1,2]
                          };

                          final logger = Logger();
                          final dio = Dio(); // Provide a dio instance
                          dio.options.headers["Content-Type"] =
                          "multipart/form-data";
                          dio.options.headers["X-Requested-With"] =
                          "XMLHttpRequest";
                          final client = RestClient(dio);

                          client.register(body,imageFile!).then((it) async {

                            print(it.access_token);

                          }).catchError((Object obj) {

                            // non-200 error goes here.
                            switch (obj.runtimeType) {
                              case DioError:
                              // Here's the sample to get the failed response error code and message
                                final res = (obj as DioError).response;
                                logger.e(
                                    "Got error : ${res!.statusCode} -> ${res.data}");
                                break;
                              default:
                            }
                          });

我不知道为什么它不起作用。没有足够的文档。谢谢你的帮助

【问题讨论】:

    标签: flutter dart retrofit


    【解决方案1】:

    不幸的是,文档没有提到这一点。
    您必须使用 @Multipart 注释您的请求才能上传文件。

    你必须注意的另一种情况是,当你使用 @Multipart 注释你的请求时,你必须使用 @Part 注释所有字段。甚至那些不是文件的。

    一个例子:

      @POST('/store')
      @MultiPart()
      Future<dynamic> store({
        @Part() required String title,
        @Part() required int part,
        @Part() File? attach,
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-31
      • 2016-09-04
      • 2017-05-14
      • 2017-07-02
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2020-05-12
      相关资源
      最近更新 更多