【问题标题】:Flutter request Instance of 'FabricType'. json.encode(fabricList)?颤振请求“FabricType”的实例。 json.encode(fabricList)?
【发布时间】:2021-01-05 09:27:30
【问题描述】:

我在多选中发送列表类型的发布请求。我无法制作 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']

问题是“编码”;问题是什么?我该怎么办?

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies flutter-test


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 2019-12-19
      • 2020-11-10
      • 1970-01-01
      • 2020-11-09
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      相关资源
      最近更新 更多