Flutter 请求 'FabricType' 的实例。 json.encode(面料清单)?

Flutter request Instance of 'FabricType'. json.encode(fabricList)?

我正在发送列表类型 post 多选请求。我在制作 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);
  }
}

Post请求

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']

问题是'encode';问题是什么?我该怎么办?

方法应该是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;
  }