问题描述
我正在多次选择中发送列表类型的帖子请求。我在制作json.encode时遇到问题。我的代码在这里。
型号
import 'dart:convert';
class FabricType {
String name;
String apiName;
int value;
String uuid;
FabricType({this.name,this.apiName,this.value,this.uuid});
factory FabricType.fromJson(Map<String,dynamic> json) {
return FabricType(
name: json['name'],apiName: json['apiName'],value: json['value'],uuid: json['uuid'],);
}
Map toMap() {
var map = new Map<String,dynamic>();
map["name"] = name;
map["apiName"] = apiName;
map["value"] = value;
map["uuid"] = uuid;
return map;
}
}
对于
List<FabricType> fabricList = List();
subContentList(List subContentList,List selectedUUid) {
fabricList.clear();
for (int i = 0; i < selectedUUid.length;i++) {
fabricType = new FabricType();
fabricType.uuid = selectedUUid[i].toString();
fabricType.name = "test";
fabricType.apiName = "colorTypes";
fabricList.add(fabricType);
}
}
发布请求
Response res = await http.post(
BaseApi + EndPoint,headers: Header().header,body:json.encode(fabricList) //Bug Here
);
print(res.body);
String statusCode = res.statusCode.toString();
print("Ad Sub Content Status Code: " + statusCode);
return res;
错误消息
I/flutter (15808): [Instance of 'FabricType',Instance of 'FabricType']
问题是“编码”;问题是什么?我该怎么办?
解决方法
方法应为toJson
,而不是toMap
,将其替换
Map<String,dynamic> toJson() {
var map = new Map<String,dynamic>();
map["name"] = name;
map["apiName"] = apiName;
map["value"] = value;
map["uuid"] = uuid;
return map;
}