List<String> nameList,
List<int> priceList,
List<String> descriptionList,
List<int> priceTypeList,
List<double> netPriceList,
List<double> taxList,
List<double> nonTaxList,
List<String> codeList,
List<String> skuList
var mapping = Map<String, dynamic>();
mapping['variants_value[Variant][]'] = nameList == null
? ""
: List<String>.from(nameList.map((String x) => x));
mapping['name[]'] = nameList == null
? ""
: List<String>.from(nameList.map((String x) => x));
mapping['duplicate_name[]'] = nameList == null
? ""
: List<String>.from(nameList.map((String x) => x));
mapping['duplicate_ids[]'] = nameList == null
? ""
: List<String>.from(nameList.map((String x) => x));
mapping['price[]'] = priceList == null
? null
: List<int>.from(priceList.map((int x) => x));
mapping['code[]'] = descriptionList == null
? ""
: List<String>.from(descriptionList.map((String x) => x));
mapping['price_type[]'] = priceTypeList == null
? null
: List<int>.from(priceTypeList.map((int x) => x));
mapping['net_price[]'] = netPriceList == null
? null
: List<double>.from(netPriceList.map((double e) => e));
mapping['tax[]'] = taxList == null
? null
: List<double>.from(taxList.map((double e) => e));
mapping['non_tax[]'] = nonTaxList == null
? null
: List<double>.from(nonTaxList.map((double e) => e));
mapping['code_type[]'] = codeList == null
? ""
: List<String>.from(codeList.map((String x) => x));
mapping['sku[]'] = skuList == null
? ""
: List<String>.from(skuList.map((String x) => x));
var url =
Uri.parse('https://xyz//m/api/product/add-product');
var response = await http.post(url,
headers: {
'Cookie': cookie,
},
body: mapping);
if (response.body != null && response.statusCode == 200) {
print(response.body);
return response.body.toString();
} else {
return null;
}
//这是我在postman
中传递的参数variants_value[Variant][]: default
name[]: default
duplicate_name[]: default
duplicate_ids[]:
price[]: 24
code[]: fgdg
price_type[]: 0
net_price[]: 24.00
tax[]: 0.00
non_tax[]: 0
code_type[]: Others
sku[]:
我正在传递Listvalue与API,它给出的错误是
"type 'List<int>' is not a subtype of type 'String' in type cast"
后来,我通过API通过了List<int>value.toString();
,我得到了这样的响应'Please enter value' Api is not accepting string
,然后我意识到值应该是int
的类型我已经检查了api在邮差它的工作良好,当我通过整数值,如果做同样的扑动它的抛出错误
请告诉我如何将List传递给API
你必须这样通过:
"list": list== null ? null : List<int>.from(list.map((int x) => x)),
编辑1:我建议你在这种情况下或一般情况下使用。
try use
mapping['name'] = jsonEncode(nameList)
而不是
mapping['name'] = List<String>.from(nameList.map((String x) => x))
到你所有的数据
根据官方文件https://pub.dev/documentation/http/latest/http/post.html
body设置请求体。它可以是String, List或a Map<String,>如果它是一个String,它使用encoding进行编码并用作请求的主体。请求的内容类型默认为"text/plain"