Flutter:多部分文件请求不起作用

问题描述

我想通过多部分文件请求上传图像。使用此代码当我传递两个图像文件时,它工作正常。但是当我想传递一个图像文件而另一个为空时,它就不起作用了。 问题出在哪儿?我该如何解决这个问题?

这是我的代码 -

Future<Map<String,dynamic>> updateprofile(
      UpdateProfileInfo updateProfileInfo,File imageFile,File signatureFile) async {

    String url = "$baseAPIUrl/update-profile-info";
    String _token = await SavedData().loadToken();
    String authorization = "Bearer $_token";

    final headers = {
      'Content-Type': 'application/json','Accept': 'application/json',"Authorization": authorization
    };

    var request = http.MultipartRequest("POST",Uri.parse(url));

    request.headers.addAll(headers);
    request.fields.addAll(updateProfileInfo.toJson());

    request.files
        .add(await http.MultipartFile.fromPath('image',imageFile.path));
    request.files.add(
        await http.MultipartFile.fromPath('signature',signatureFile.path));

    print(" Update Profile Json ${updateProfileInfo.toJson()}");
    print("Request Fields ${request.fields}");
    http.StreamedResponse response = await request.send();

    String respStr = await response.stream.bytesToString();
    dynamic respJson;

    try {
      respJson = jsonDecode(respStr);
    } on FormatException catch (e) {
      print(e.toString());
    }

    print('API ${response.statusCode}\n  $respJson');

    bool isSuccess = response.statusCode == 200;
    var data = json.decode(respStr);

    return {
      'isSuccess': isSuccess,"message": isSuccess ? data["success"]["message"] : null,"name": isSuccess ? data["success"]["name"] : null,"classgroup": isSuccess ? data["success"]["classgroup"] : null,"image": isSuccess ? data["success"]["image"] : null,"error": isSuccess ? null : data['error']['message'],};
  }

这是邮递员截图

1.

enter image description here

2. POSTMAN 为 Dart 生成的代码 - http

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)