问题描述
我为带有上传图像的 Multipart API post 请求创建了一个 Future 函数。但是当我使用 request.fields.addAll(updateProfileInfo.toJson()); 时,它没有响应。
这是代码示例 -
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'],};
}
如何发送正文字段请求?请有人帮助我
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)