我尝试使用 Dio 在 Web 中使用 Flutter 上传文件。我使用
file_picker
获取文件并获取文件的字节。然后我尝试将文件上传到后端,但收到错误消息 Instance of 'JSArray<int>': type 'List<int>' is not a subtype of type 'Uint8List'
这是我的代码。最后一行抛出错误。
似乎由于某种原因,当它试图理解类型为
file.bytes
的 List<int>
时,它无法将其转换为 Uint8List
。
如有任何建议,我们将不胜感激。
final body = <String, dynamic>{};
if (fields != null) {
body.addAll(fields);
}
body.addAll({
"files": files
.map((file) => MultipartFile.fromBytes(
file.bytes,
filename: file.fileName,
))
.toList()
});
final formData = FormData.fromMap(body);
Response<Map<String, dynamic>> response =
await dio.postUri<Map<String, dynamic>>(url, data: formData, options: options);
首先告诉我两件事:
你想在 flutter web 上运行这个吗?
您在 body.addAll 中传递的文件变量的类型是什么。
此外,请提供更多代码片段,这有助于更好地理解问题。