You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter发送POST Multipart请求时如何在请求体中携带List类型参数

Flutter MultipartRequest 传递复杂业务参数解决方案

http.MultipartRequest的fields属性仅支持Map<String, String>类型,无法直接传递嵌套对象、数组类的复杂结构,可通过以下两种方案解决:

方案一:序列化复杂参数为JSON字符串(改造成本最低)

将业务参数整体序列化为JSON字符串作为单个表单字段传递,后端只需对应反序列化即可,是最常用的处理方式:

import 'dart:convert';
import 'package:http/http.dart' as http;

// 构造业务参数
Map<String, dynamic> bodyMap = {
  'type' : 'text',
  'content': [
     {
        'type': 'image',
        'identifier': '1234'
     }
   ]
};
// 序列化为JSON字符串
String businessParams = jsonEncode(bodyMap);

// 构造Multipart请求
var request = http.MultipartRequest('POST', Uri.parse(url));
// 添加序列化后的业务参数
request.fields['businessParams'] = businessParams;

// 添加要上传的图片文件(示例)
request.files.add(await http.MultipartFile.fromPath(
  'image', // 后端接收文件的字段名
  '本地图片的实际路径',
  filename: 'upload_image.jpg'
));

// 发起请求
var response = await request.send();

方案二:扁平化嵌套参数(适配严格表单传参要求)

如果后端要求严格按嵌套层级传递表单参数,可以将复杂结构按照表单数组、对象的命名规则平铺为字符串键值对:

var request = http.MultipartRequest('POST', Uri.parse(url));
// 平铺嵌套业务参数
request.fields['type'] = 'text';
request.fields['content[0][type]'] = 'image';
request.fields['content[0][identifier]'] = '1234';

// 添加上传图片
request.files.add(await http.MultipartFile.fromPath(
  'image',
  '本地图片的实际路径',
));

内容的提问来源于stack exchange,提问作者Ammar Mohamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 18:54:04