简体   繁体   中英

Flutter : display data from json

i have a json data, i want to display it in my flutter app,so how can i display data from [bookings] partie,for now its easy i use this data[0]['now']['location'] , but i have a difficulty to find a way to display data from booking . can someone help me plz

[
{
"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"
}
]
]
]

Create a named constructor fromJson which accepts your json data and returns a list of Books (see here: 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,
      };
}

First, you create a model of your object. and create JSON file and put it in your project.

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

You can get hear your JSON data and after that, you can initialise your JSON data to the model. like this

  var objProductDetails = ProductDetailsDataModel.fromJson(jsonResult);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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