[英]Can't access Nested array object flutter
我有一組數據,有一些細節,當我嘗試顯示一個值返回 null,其他 2 個數據很好,但是如果我嘗試顯示其他數據,它顯示 null,如果我嘗試將其添加到 setState,一切變成null,當我得到“描述”,“圖像路徑”時沒有問題我可以顯示它,但是來自回復object的數據沒有顯示
JSON
{
"doc": {
"image": {
"Description": "tested",
"replay": " ",
"Image_Rating": 0,
"replay_status": 0,
"Report_Date": "1591228800",
"Status": 1,
"_id": "5ed88ae73025a4445568ece3",
"image_path": "http://xxx.xxx.xxx.xxx:xxx/area_images/1670281356001.jpg",
"Created_User_Id": "5ed22c2507a33e2c1cf3a3a5",
"Branch_Id": "5ed22bf807a33e2c1cf3a3a4",
"image_temp_path": "http://xxx.xxx.xxx.xxx:xxx/area_images_temp/1670281356001.jpg",
"Order_Id": 32425,
"reg_date": "1591249638163",
"Area_Id": "5dc11c4046c214298f85e2e0",
"Section_Id": "5dc1097546c214298f85e2ae",
"Report_Time_Type": 1,
"mapperId": "5ed22c4207a33e2c1cf3a3a6",
"Created_At": "Thursday, June 4th, 2020, 11:17:18 AM",
"__v": 0
},
"replays": [
{
"replay": "Good\n",
"Report_Date": "1590796800",
"_id": "5ed248e0c1a47a3e8c4ce8bb"
}
]
}
}
代碼
Future<String> getImageView(String imageid) async {
Future token = SharedPrefrence().getToken();
token.then((data) async {
var token = data;
var response = await http.post(Urls.Image_Details,
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer $token",
},
body: json.encode({
"imageId": imageid,
}));
if (response.statusCode == 200) {
try {
var resp = response.body;
Map<String, dynamic> value = json.decode(resp);
var name = value['doc']['image'];
Description = name["Description"].toString();
image_path = name["image_path"].toString();
replay = name["replays"]["replay"].toString();
setState(() {
Description = name["Description"].toString();
image_path = name["image_path"].toString();
// replay = name["replays"]["replay"].toString();
});
} catch (e) {
e.toString();
}
}
});
}
"replays"
是一個數組。 試試這個: name["replays"][0]["replay"].toString()
通過添加[0]
,它將從該數組中獲得您的第一個 object。
編輯:在查看了您的 json 之后,我發現該name
是錯誤的 object。 "replays"
是"doc"
的成員,而不是"image"
的成員。
我認為這應該有效:
replay = value['doc']["replays"][0]["replay"].toString();
問題是"replays": [ { "replay": "Good\n", "Report_Date": "1590796800", "_id": "5ed248e0c1a47a3e8c4ce8bb" } ]
這是地圖列表。 因此,當我們訪問列表中的第一個元素時,您應該使用
replay= value['doc']["replays"][0]["replay"].toString();
這是列表的第零個元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.