简体   繁体   English

Flutter:未处理的异常:无效的参数

[英]Flutter : unhandled exception: invalid argument

I'm trying to send two list of images file and i have an error with that and i don't know why this happen this is the function which i use for that:我正在尝试发送两个图像文件列表,但我有一个错误,我不知道为什么会发生这种情况,这是我使用的 function:

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;
      }
    }
  }

and in the part of并且在

var response = await request.send();

I have the error exactly before the dot (.) and this is the error which shown我正好在点(。)之前有错误,这是显示的错误

unhandled exception: invalid argument(s) (input): must not be null Can anyone tell me what should i do to solve that !未处理的异常:无效参数(输入):不能是 null 谁能告诉我我该怎么做才能解决这个问题!

Send your Multipart Part Files as below and Use await http.Multipart as accessing file is async function and need a promise to run.如下发送您的多部分文件并使用await http.Multipart因为访问文件是异步 function并且需要 promise 才能运行。

Try this:尝试这个:

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

OR或者

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

Follow this link 按照这个链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 错误:未处理的异常:RangeError(索引):无效值:有效值范围为空:0 - Flutter Error : Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0 未处理的异常呈现组件:“&lt;”是一个无效的值开始 - Unhandled exception rendering component: '<' is an invalid start of a value Flutter 中的错误:未处理的异常:“Null”类型不是“String”类型的子类型 - error in Flutter : Unhandled Exception: type 'Null' is not a subtype of type 'String' Flutter:未处理的异常:失败的断言:布尔表达式不能为空 - Flutter: unhandled Exception: Failed assertion: boolean expression must not be null 未处理的异常:类型“int”不是 Flutter 应用程序中“字符串”类型的子类型 - Unhandled Exception: type 'int' is not a subtype of type 'String' in Flutter app 无效参数:URI 文件中未指定主机 flutter api 问题 - Invalid argument(s): No host specified in URI file flutter api problem 例外:无效参数 - API Weather Current Data Pull - Exception: Invalid Argument - API Weather Current Data Pull Flutter 未处理的异常:DioError [DioErrorType.DEFAULT]:NoSuchMethodError:getter &#39;statusCode&#39; 在 null 上被调用 - Flutter Unhandled Exception: DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The getter 'statusCode' was called on null 未处理的异常:键入“列表”<dynamic> &#39; 不是类型 &#39;Map 的子类型<dynamic, dynamic> &#39;在颤抖中 - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<dynamic, dynamic>' in flutter Flutter,无法提取 api 数据:(未处理的异常:NoSuchMethodError:方法 &#39;map&#39; 在 null 上被调用。) - Flutter, can't extract api data : (Unhandled Exception: NoSuchMethodError: The method 'map' was called on null.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM