DioErrorType.RESPONSE:Http状态错误[500](Flutter)

问题描述 投票:0回答:1

我正在尝试发布图像和数据,但是我得到了这个错误,请帮助。我正在使用dio:^ 3.0.9。在邮递员中工作正常,并且如果我从数据中删除图像并仅发布数据,则工作正常。

class ProfileRepository {
  LocalStorage localStorage = new LocalStorage();
  Dio dio = new Dio();

  final url = 'http://localhost:3000/api/profile';

  Future<ProfileModel> getProfileResponse(
      String fullName,
      String position,
      String gender,
      String homeAddress,
      String officeAddress,
      String phoneNumber,
      File image) async {
    final token = await localStorage.getLoginToken();
    final splitToken = token.split(' ');
    final finalToken = splitToken[1];

    String imageFile = image.path.split('/').last;
    var formData = new FormData.fromMap({
      "fullname": fullName,
      "position": position,
      "gender": gender,
      "home_address": homeAddress,
      "office_address": officeAddress,
      "phone_no": phoneNumber,
      "image": await MultipartFile.fromFile(image.path, filename: imageFile),
    });

    final response = await dio.post(url,
        data: formData,
        options: Options(headers: {'Authorization': finalToken}));

    if (response.statusCode == 201) {
      return ProfileModel.fromJson(json.jsonDecode(response.data));
    } else {
      throw Exception("Error");
    }
  }
}
flutter http-status-code-500
1个回答
0
投票
final url = 'http://localhost:3000/api/profile';

  Future<ProfileModel> getProfileResponse(
      String fullName,
      String position,
      String gender,
      String homeAddress,
      String officeAddress,
      String phoneNumber,
      File image) async {
    final token = await localStorage.getLoginToken();
    final splitToken = token.split(' ');
    final finalToken = splitToken[1];

    String imageFile = image.path.split('/').last;
    FormData formData() {
     var formData = new FormData.fromMap({
      "fullname": fullName,
      "position": position,
      "gender": gender,
      "home_address": homeAddress,
      "office_address": officeAddress,
      "phone_no": phoneNumber,
    });
      formData.files.addAll([
          MapEntry(
              "files",
              MultipartFile.fromFileSync(image.path, filename: imageFile)),
      ]);
      return formData;
    }
    final response = await dio.post(url,
        data: formData(),
        options: Options(headers: {'Authorization': finalToken}));

    if (response.statusCode == 201) {
      return ProfileModel.fromJson(json.jsonDecode(response.data));
    } else {
      throw Exception("Error");
    }
  }
}

尝试一下。

© www.soinside.com 2019 - 2024. All rights reserved.