Flutter:发送字段时 API 没有响应?

Flutter : API isn't respond when send Field?

我为带有上传图片的 Multipart API post 请求创建了一个 Future 函数。但是当我使用 request.fields.addAll(updateProfileInfo.toJson()); 它没有响应。

Here is the code sample -

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

如何发送正文字段请求?请有人帮助我

request.fields<String, String> 类型的地图。所以你所有的字段数据都必须是字符串。换句话说,updateProfileInfo.toJson() 必须 return Map<String, String> type object.

希望这可以解决您的问题。