api - flutter : unhandled exception: invalid argument

标签 api flutter dart unhandled-exception

我正在尝试发送两个图像文件列表,但出现错误,我不知道为什么会发生这种情况 这是我使用的函数:

Future<dynamic> upload(List<File> imageFile, List<File> backID) async {

    var headers =
        Provider.of<LoginUserProvider>(context, listen: false).httpHeader;
    var uri = Uri.parse(
        "xyz");

    var request = new http.MultipartRequest("POST", uri);
    var requestCopy = new http.MultipartRequest("POST", uri);
    request.headers.addAll(headers);
    //request.headers.addAll(Environment.requestHeaderMedia);
    for (int i = 0; i < uploadedFilesList.length; i++) {
      var length = await imageFile[i].length();
      var stream =
          // ignore: deprecated_member_use
          new http.ByteStream(DelegatingStream.typed(imageFile[i].openRead()));
      var multipartFile = http.MultipartFile(
        'back_id_photos[$i]',
        stream,
        length,
        contentType: MediaType('application', 'x-tar'),
      );
      var multipartFilePhotos = new http.MultipartFile(
        'photos[$i]',
        stream,
        length,
        contentType: MediaType('application', 'x-tar'),
      );
      request.fields['section_id'] = VillaID.toString();
      request.fields['start_date'] = requeststartDate;
      request.fields['end_date'] = requestendDate;
      request.fields['names'] = _nameController.toString();
      request.fields['phones'] = _phoneController.toString();
      request.fields['photos'] = multipartFilePhotos.toString();
      request.fields['back_id_photos'] = multipartFile.toString();
      //contentType: new MediaType('image', 'png'));

      request.files.add(multipartFile);
      request.files.add(multipartFilePhotos);
      var response = await request.send();
      print(response.statusCode);
      response.stream.transform(utf8.decoder).listen((value) {
        print(value);
      });
      var streamedResponse = await requestCopy.send();
      var responses = await http.Response.fromStream(streamedResponse);
      // ignore: unused_local_variable
      final responseData = json.decode(responses.body) as Map<String, dynamic>;
      print(json.decode(responses.body));
      if (response.statusCode == 200 || response.statusCode == 201) {
        return true;
      }
    }
  }

以及部分

var response = await request.send();

我在点 (.) 之前出现了错误,这就是显示的错误

unhandled exception: invalid argument(s) (input): must not be null Can anyone tell me what should i do to solve that !

最佳答案

按如下方式发送您的多部分文件,并使用 await http.Multipart 因为访问文件是异步函数,并且需要 promise 才能运行。

试试这个:

await http.MultipartFile(
    'back_id_photos[$i]',
    stream,
    length,
    contentType: MediaType('application', 'x-tar'),
  );

或者

request.files.add(new http.MultipartFile.fromBytes('file', await File.fromUri("<path/to/file>").readAsBytes(), contentType: new MediaType('image', 'jpeg')))

Follow this link

关于api - flutter : unhandled exception: invalid argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65202121/

相关文章:

c - protected 共享内存?

javascript - JSON 中未定义值的尝试/捕获失败 - JavaScript

flutter -> 找不到 com.google.gms.google-services :4. 3.15 :. 要求者:项目:

Flutter integration_test 包与我的代码冲突,无法通过调整包版本来解决 - 因此整个应用程序无法运行

javascript - 如何更改 Darksky API 请求的时间格式?

flutter - 使用网格布局的文本和文本字段

flutter - 从主题更改 RaisedButton 的颜色不起作用

Dart 名称冲突解决

flutter - Firestore 更改后使用 FCM 发送推送通知?

swift - 如何将两个参数传递给webservice