繁体   English   中英

Flutter:显示来自 json 的数据

[英]Flutter : display data from json

我有一个 json 数据,我想在我的 flutter 应用程序中显示它,那么我如何显示来自[bookings]派对的数据,现在我很容易使用这个data[0]['now']['location'] ,但我很难找到一种显示booking数据的方法。 有人可以帮我吗?

[
{
"now":{
"id": 28,
"userId": 6,
"bookingTypeId": 2,
"carWasherId": 26,
"buildingId": 4,
"serviceId": 1,
"packageId": 1,
"vehiculeId": 7,
"tookenPackageId": null,
"status": "ACCEPTED",
"dateTime": "2021-12-18 12:30:00",
"date": "2021-12-18",
"address": null,
"longitude": -6.7436544,
"latitude": 33.9968,
"rate": null,
"created_at": "2021-10-29T14:26:16.000000Z",
"updated_at": "2021-11-10T15:02:33.000000Z",
"location": "",
"duration": 30,
"start": "12:30",
"end": "13:00"
}
},
[
"bookings",
[
{
"id": 29,
"userId": 6,
"bookingTypeId": 1,
"carWasherId": 26,
"buildingId": 2,
"serviceId": 1,
"packageId": null,
"vehiculeId": 2,
"tookenPackageId": null,
"status": "ACCEPTED",
"dateTime": "2021-12-18 09:47:33",
"date": "2021-12-18",
"address": null,
"longitude": null,
"latitude": null,
"rate": null,
"created_at": "2021-10-29T15:03:14.000000Z",
"updated_at": "2021-11-10T15:00:25.000000Z",
"location": "",
"duration": 20,
"start": "09:47",
"end": "10:07"
},
{
"id": 30,
"userId": 6,
"bookingTypeId": 2,
"carWasherId": 26,
"buildingId": 3,
"serviceId": 1,
"packageId": 1,
"vehiculeId": 7,
"tookenPackageId": null,
"status": "ACCEPTED",
"dateTime": "2021-12-22 21:30:00",
"date": "2021-12-22",
"address": null,
"longitude": -6.7436544,
"latitude": 33.9968,
"rate": null,
"created_at": "2021-10-29T15:18:45.000000Z",
"updated_at": "2021-11-10T22:16:47.000000Z",
"location": "",
"duration": 25,
"start": "21:30",
"end": "21:55"
}
]
]
]

从Json 创建一个命名构造函数,它接受您的fromJson数据并返回书籍列表(请参见此处: https://docs.flutter.dev/development/data-and-backend/json

class Book{
  final String name;
  final String id;

  User(this.name, this.id);

  User.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        id= json['id'];

  Map<String, dynamic> toJson() => {
        'name': name,
        'id': id,
      };
}

首先,创建 object 的 model。 并创建 JSON 文件并将其放入您的项目中。

String productData = await DefaultAssetBundle.of(context).loadString("assets/product.json");
        final jsonResult = jsonDecode(productData);

您可以听到 JSON 数据,然后,您可以将 JSON 数据初始化为 model。 像这样

  var objProductDetails = ProductDetailsDataModel.fromJson(jsonResult);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM