0

我已经为我的 Fluter 项目配置了BLoC+ ,并且到目前为止一切正常。目前,集成上传,我从侧面面临一些问题,我不知道它是什么以及如何解决这个问题。ChopperAPIFileAPIChopper

@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);
  },
);

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

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

2 回答 2

0

这个解决方案对我有用:

函数调用

await _apiService.uploadVendorPhoto(_profileImage!.path);

API

@Post(path: 'apiPath/uploadPhoto', headers: {"Content-Type": "multipart/formdata"})
  @multipart
  Future<Response> uploadPhoto(@PartFile('image') String image);
于 2021-09-30T16:15:00.600 回答
0

我确实在通话之间修改了这个 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<bytes>对我不起作用,但File路径帮助我,

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

希望这会对某人有所帮助。

于 2020-03-24T06:35:34.090 回答