Flutter 使用多图像选择器http进行颤振上载图像

Flutter 使用多图像选择器http进行颤振上载图像,flutter,http,flutter-dependencies,Flutter,Http,Flutter Dependencies,我有这个代码试图上传图像与颤振 代码成功地从库中拾取多个图像并显示,但无法成功上载 这是你的电话号码 未处理的异常:错误状态:无法设置内容类型为“multipart/form data”的请求的正文字段 List image=List(); 字符串_error='未检测到错误'; Widget buildGridView(){ 返回GridView.count( 交叉轴计数:3, 子项:List.generate(images.length,(index){ 资产=图像[索引]; 归还资产( 资

我有这个代码试图上传图像与颤振 代码成功地从库中拾取多个图像并显示,但无法成功上载 这是你的电话号码

未处理的异常:错误状态:无法设置内容类型为“multipart/form data”的请求的正文字段

List image=List();
字符串_error='未检测到错误';
Widget buildGridView(){
返回GridView.count(
交叉轴计数:3,
子项:List.generate(images.length,(index){
资产=图像[索引];
归还资产(
资产:资产,,
宽度:300,
身高:300,
);
}),
);
}
未来上载资产()异步{
List multipart=List();
对于(int i=0;iimage.getByteData());
//
//List byteArrayList=byteDataList.map((ByteData ByteData){
//ByteBuffer ByteBuffer=byteData.buffer;
//返回byteBuffer.asUint8List(byteData.offsetInBytes,byteBuffer.lengthInBytes);
//}).toList();
//地图数据={
//:多部分,
//'说明':'此处',
//“大小”:“7899”
//
//    };
var res=等待httpSend(多部分);
印刷品(res);
}
Future loadAssets()异步{
List resultList=List();
字符串错误='未检测到错误';
试一试{
结果列表=等待MultiImagePicker.pickImages(
最高限额:300,
启用摄像头:正确,
所选资产:图像,
cupertinoOptions:cupertinoOptions(takePhotoIcon:“聊天”),
材质选项:材质选项(
actionBarColor:#abcdef“,
actionBarTitle:“示例应用程序”,
allViewTitle:“所有照片”,
useDetailsView:false,
选择CircleStrokeColor:#000000,
),
);
}关于异常捕获(e){
错误=e.toString();
}
如果(!已安装)返回;
设置状态(){
图像=结果列表;
_错误=错误;
});
}
归还新脚手架(
主体:新容器(
子:容器(
子:堆栈(
儿童:[
可见度(
可见:_pickvisible,
子:容器(
填充物:LTRB(20.0180.0,0.0,0.0)的边缘设置,
//子:堆栈(
子:列(
儿童:[
居中(子项:文本('Error:$\u Error')),
升起的按钮(
子项:文本(“拾取图像”),
onPressed:loadAssets,
),
升起的按钮(
子:文本(“上载”),
onPressed:上传资产,
),
扩大(
子级:buildGridView(),
)
],
),
),
),
],
),
);
});
}
}
我正在尝试使用http上传多个图像


Future httpSend(List<http.MultipartFile> params) async
{
  Map data ={
    'file':params
  };
  String endpoint = 'https://dynamicurl.com/api/uploadimage';
  final response= await http.post(endpoint, body: data,
    headers:{ "Content-Type":"multipart/form-data" } );
  List<dynamic> convertedDatatoJson = null;
  if(response.body.isEmpty){

    convertedDatatoJson =  null;
  }else{
    print(response.body);
    convertedDatatoJson =  jsonDecode(response.body);
  }
  return convertedDatatoJson;
}



未来httpSend(列表参数)异步
{
地图数据={
“文件”:参数
};
字符串端点https://dynamicurl.com/api/uploadimage';
最终响应=等待http.post(端点,主体:数据,
标题:{“内容类型”:“多部分/表单数据”});
List convertedDatatoJson=null;
if(response.body.isEmpty){
convertedDatatoJson=null;
}否则{
打印(响应.正文);
convertedDatatoJson=jsonDecode(response.body);
}
返回convertedDatatoJson;
}
这里是错误 未处理的异常:错误状态:无法设置内容类型为“multipart/form data”的请求的正文字段。
我想看看如何上传多个图像的解决方案,首先,将列表转换为列表,错误是我工作的结果。

您可以使用图像选择器和Dio

创建获取图像列表的方法

Future<List<Media>> getListImage() async {
return await ImagesPicker.pick(
  count: 3,
  pickType: PickType.image,
); }
Future<List<Media>> getListImage() async {
return await ImagesPicker.pick(
  count: 3,
  pickType: PickType.image,
); }
Future<void> uploadFile(String filePath) async {
    var filename = filePath.split("/").last;

    var formData = FormData.fromMap({
      "file": await MultipartFile.fromFile(filePath, filename: filename)
    });

    Dio dio = new Dio();
    var response = await dio.post(YOUR_URL, data: formData);

    print(response);
  }
var list = await getListImage();
    
list.forEach((element) async {
    
   await uploadFile(element.path);
    
});