【问题标题】:Flutter Chopper File Upload API throw NoSuchMethodErrorFlutter Chopper 文件上传 API 抛出 NoSuchMethodError
【发布时间】:2020-06-30 07:23:41
【问题描述】:

我已经为我的 Fluter 项目配置了 BLoC + Chopper 并且所有 API 工作正常至今。目前,正在集成File 上传API,我面临来自Chopper 方面的一些问题,我不知道它是什么以及如何解决。

@Post(path: "FILE_UPLOAD_URL")
@multipart
Future<Response<ProfileResponse>> uploadUserProfilePic(
  @Header("Authorization") String token,
  @PartFile('file') List<int> file,
);

API 调用:

await SharedPreferenceHelper.getToken().then(
  (token) async {
    final bytes = (await File(event.file.path).readAsBytes()).toList();
    final file = http.MultipartFile.fromBytes('file', bytes);
    profileResponse =
        await Provider.of<ApiService>(context, listen: false)
            .uploadUserProfilePic(token, bytes);
  },
);

从这个方法和文件 (object_patch.dart) 中抛出 NoSuchMethodError 异常,

@patch
@pragma("vm:entry-point", "call")
dynamic noSuchMethod(Invocation invocation) {
  // TODO(regis): Remove temp constructor identifier 'withInvocation'.
  throw new NoSuchMethodError.withInvocation(this, invocation);
}

【问题讨论】:

    标签: flutter chopper


    【解决方案1】:

    我确实在两次通话之间修改了这个 requestConverter,

    @override
      Request convertRequest(Request request) {
        if (!request.multipart) {
          return super.convertRequest(
            request.replace(
              body: serializers.serializeWith(
                serializers.serializerForType(request.body.runtimeType),
                request.body,
              ),
            ),
          );
        } else {
          return request;
        }
      }
    

    List&lt;bytes&gt; 对我不起作用,但File 路径帮助我,

    @Post(
        path: URLS.API_UPLOAD_IMAGE,
        headers: {"Content-Type": "multipart/formdata"},
      )
      @multipart
      Future<Response> uploadUserProfilePic(
        @Header("Authorization") String token,
        @PartFile('file') String file,
      );
    

    希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      这个解决方案对我有用:

      函数调用

      await _apiService.uploadVendorPhoto(_profileImage!.path);
      

      API

      @Post(path: 'apiPath/uploadPhoto', headers: {"Content-Type": "multipart/formdata"})
        @multipart
        Future<Response> uploadPhoto(@PartFile('image') String image);
      

      【讨论】:

        猜你喜欢
        • 2021-08-14
        • 2018-10-25
        • 2020-02-22
        • 2015-03-21
        • 2020-02-09
        • 1970-01-01
        • 2014-07-07
        • 2020-09-09
        • 1970-01-01
        相关资源
        最近更新 更多