Flutter请求“ FabricType”的实例 json.encodefabricList?

问题描述

我正在多次选择中发送列表类型的帖子请求。我在制作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;
  }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...