我有一個 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"
}
]
]
]
uj5u.com熱心網友回復:
創建一個命名建構式fromJson,它接受您的 json 資料并回傳書籍串列(請參閱此處: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,
};
}
uj5u.com熱心網友回復:
首先,您創建物件的模型。并創建 JSON 檔案并將其放入您的專案中。
String productData = await DefaultAssetBundle.of(context).loadString("assets/product.json");
final jsonResult = jsonDecode(productData);
您可以聽到您的 JSON 資料,然后,您可以將您的 JSON 資料初始化到模型中。像這樣
var objProductDetails = ProductDetailsDataModel.fromJson(jsonResult);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377571.html
上一篇:Firebaseflutter_CastError(用于空值的空檢查運算子)
下一篇:計算輸出不正確
