gpt4 book ai didi

flutter - 无法将 rest api json 转换为 dart 对象

转载 作者:IT王子 更新时间:2023-10-29 06:59:10 33 4
gpt4 key购买 nike

在使用 jsondecode 转换 rest api 响应后无法循环

我有来自 laravel 后端的 rest api,我正在尝试使用 flutter 应用程序。使用 jsondecode 转换响应后无法遍历它并制作一个列表以在小部件 View 中使用

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(s)).toList();

没有返回 View

最佳答案

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

关于flutter - 无法将 rest api json 转换为 dart 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976670/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com