无法将其余api json转换为dart对象

时间:2018-12-30 09:59:06

标签: flutter

在使用jsondecode转换其余api响应后无法循环遍历

我有来自laravel后端的rest api,我尝试被flutter应用程序消耗。 在使用jsondecode转换响应后无法循环遍历并列出要在小部件视图中使用的列表

api Json响应:

{
    "success": true,
    "data": [
        {
            "id": 1,
            "user_id": 1,
            "mobile_num": "34567",
            "date_before": "2018-12-20 00:00:00",
            "cargo_id": 3,
            "weight": 12,
            "description": "medical stoer",
            "country_id": 22,
            "deleted_at": null,
            "created_at": "2018-12-25 10:42:35",
            "updated_at": "2018-12-25 10:42:35"
        },
        {
            "id": 2,
            "user_id": 3,
            "mobile_num": "21345",
            "date_before": "2018-12-12 00:00:00",
            "cargo_id": 3,
            "weight": 3,
            "description": "welcome by the",
            "country_id": 3,
            "deleted_at": null,
            "created_at": "2018-12-25 10:43:02",
            "updated_at": "2018-12-25 10:43:02"
        },
        {
            "id": 3,
            "user_id": 2,
            "mobile_num": "2342344543",
            "date_before": "2018-12-12 00:00:00",
            "cargo_id": 5,
            "weight": 44,
            "description": "hello maseer",
            "country_id": 3,
            "deleted_at": null,
            "created_at": "2018-12-25 10:44:16",
            "updated_at": "2018-12-25 10:44:16"
        }
    ],
    "message": "Senders retrieved successfully"
}

http获取响应代码:

  Future<dynamic> _getData(String url) async {
    var response = await http.get(url);
    var data = json.decode(response.body);
    return data;
  }

 Future<List<Sender>> getSenders() async {
    var data = await _getData(_baseUrl);
    List<dynamic> sendersData = data['data'];
    print(sendersData);
    print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    //code stop in this point not loop through sendersData***********
    List<Sender> senders =
        sendersData.map((s) => Sender.fromMap(s)).toList();
    print("yyyyyyyyyyyyyyyyyyyyyyyyyyy");
    print(senders);
    // print(senders.length);
    return senders;
  }

型号代码:

class Sender {
  String id,
      userId,
      mobileNum,
      dateBefore,
      cargoId,
      weightS,
      descriptionS,
      countryId,
      createdAt;
  Sender({
    this.id,
    this.userId,
    this.mobileNum,
    this.dateBefore,
    this.cargoId,
    this.weightS,
    this.descriptionS,
    this.countryId,
    this.createdAt,
  });

  Sender.fromMap(Map<String, dynamic> map) {
    id = map['id'];
    userId = map['user_id'];
    mobileNum = map['mobile_num'];
    dateBefore = map['date_before'];
    cargoId = map['cargo_id'];
    weightS = map['weight'];
    descriptionS = map['description'];
    countryId = map['country_id'];
    createdAt = map['created_at'];
  }
}

此代码未执行:发件人=         sendersData.map((s)=> Sender.fromJson).toList();

不返回视图

2 个答案:

答案 0 :(得分:1)

id,userId等是整数值,而不是字符串。

    class Sender {
  int id;
  int userId;
  String mobileNum;
  String dateBefore;
  int cargoId;
  int weight;
  String description;
  int countryId;
  String deletedAt;
  String createdAt;
  String updatedAt;

  Sender(
      {this.id,
      this.userId,
      this.mobileNum,
      this.dateBefore,
      this.cargoId,
      this.weight,
      this.description,
      this.countryId,
      this.deletedAt,
      this.createdAt,
      this.updatedAt});

  Sender.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    mobileNum = json['mobile_num'];
    dateBefore = json['date_before'];
    cargoId = json['cargo_id'];
    weight = json['weight'];
    description = json['description'];
    countryId = json['country_id'];
    deletedAt = json['deleted_at'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['mobile_num'] = this.mobileNum;
    data['date_before'] = this.dateBefore;
    data['cargo_id'] = this.cargoId;
    data['weight'] = this.weight;
    data['description'] = this.description;
    data['country_id'] = this.countryId;
    data['deleted_at'] = this.deletedAt;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

结果

flutter: [{id: 1, user_id: 1, mobile_num: 34567, date_before: 2018-12-20 00:00:00, cargo_id: 3, weight: 12, description: medical stoer, country_id: 22, deleted_at: null, created_at: 2018-12-25 10:42:35, updated_at: 2018-12-25 10:42:35}, {id: 2, user_id: 3, mobile_num: 21345, date_before: 2018-12-12 00:00:00, cargo_id: 3, weight: 3, description: welcome by the, country_id: 3, deleted_at: null, created_at: 2018-12-25 10:43:02, updated_at: 2018-12-25 10:43:02}, {id: 3, user_id: 2, mobile_num: 2342344543, date_before: 2018-12-12 00:00:00, cargo_id: 5, weight: 44, description: hello maseer, country_id: 3, deleted_at: null, created_at: 2018-12-25 10:44:16, updated_at: 2018-12-25 10:44:16}]
flutter: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
flutter: yyyyyyyyyyyyyyyyyyyyyyyyyyy
flutter: [Instance of 'Sender', Instance of 'Sender', Instance of 'Sender']

答案 1 :(得分:0)

getSenders方法中创建地图列表的方式错误。我将注释掉需要替换的行,并放置一些将起作用的代码。 确保您还在Sender.fromMap(...)方法中处理正确的数据类型。

Future<List<Sender>> getSenders() async {
var data = await _getData(_baseUrl);
//List<dynamic> sendersData = data['data']; // replace this line with
List< Map<String, dynamic> > sendersData = List.from( data['data'] );
print(sendersData);
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
List<Sender> senders = sendersData.map((s) => Sender.fromMap(s)).toList();

print("yyyyyyyyyyyyyyyyyyyyyyyyyyy");
print(senders);
// print(senders.length);
return senders;

}