颤振:API不是';发送字段时不响应?

颤振:API不是';发送字段时不响应?,api,flutter,response,multipart,Api,Flutter,Response,Multipart,我为带有上传图像的多部分API post请求创建了一个未来函数。但是当我使用request.fields.addAll(updateProfileInfo.toJson())时它没有响应 下面是代码示例- 未来更新配置文件( UpdateProfileInfo UpdateProfileInfo,文件imageFile)异步{ var stream=http.ByteStream(stream.castFrom(imageFile.openRead()); var length=wait ima

我为带有上传图像的多部分API post请求创建了一个未来函数。但是当我使用request.fields.addAll(updateProfileInfo.toJson())时它没有响应

下面是代码示例-

未来更新配置文件(
UpdateProfileInfo UpdateProfileInfo,文件imageFile)异步{
var stream=http.ByteStream(stream.castFrom(imageFile.openRead());
var length=wait imageFile.length();
字符串url=“$baseAPIUrl/更新配置文件信息”;
字符串_token=wait SavedData().loadToken();
String authorization=“Bearer$\u token”;
最终正文=JSONECODE(updateProfileInfo);
最终标题={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
“授权”:授权
};
var request=http.MultipartRequest(“POST”,Uri.parse(url));
var multipartFile=http.multipartFile('image',stream,length,
文件名:basename(imageFile.path));
request.headers.addAll(headers);
request.files.add(多部分文件);
addAll(updateProfileInfo.toJson());
http.streamdresponse response=等待请求。发送();
String respStr=wait response.stream.bytesToString();
动态响应;
试一试{
respJson=jsonDecode(respStr);
}关于格式化异常捕获(e){
打印(如toString());
}
打印('API${response.statusCode}\n$respJson');
bool issucess=response.statusCode==200;
var data=json.decode(respStr);
返回{
“isSuccess”:isSuccess,
“消息”:isSuccess?数据[“成功”][“消息”]:空,
“名称”:isSuccess?数据[“成功”][“名称”]:空,
“类组”:isSuccess?数据[“成功”][“类组”]:空,
“图像”:isSuccess?数据[“成功”][“图像”]:空,
“错误”:isSuccess?空:数据['error']['message'],
};
}

如何发送正文字段请求?请有人帮我请求。字段是
类型的映射。因此,所有字段数据都必须是字符串。换句话说,
updateProfileInfo.toJson()
必须返回
Map
类型对象

希望这能解决你的问题

    Future<Map<String, dynamic>> updateprofile(
      UpdateProfileInfo updateProfileInfo, File imageFile) async {
    var stream = http.ByteStream(Stream.castFrom(imageFile.openRead()));
    var length = await imageFile.length();

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

    final body = jsonEncode(updateProfileInfo);
    final headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      "Authorization": authorization
    };

    var request = http.MultipartRequest("POST", Uri.parse(url));
    var multipartFile = http.MultipartFile('image', stream, length,
        filename: basename(imageFile.path));
    request.headers.addAll(headers);
    request.files.add(multipartFile);

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

    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'],
    };
  }