简体   繁体   中英

Can't access Nested array object flutter

I have set of data, with some details,when i try to display the the one value returns null,other 2 data is fine,but if i try to show other data it's shows null,if i try to add that to setState,everything become null,There is no problem when i get the "Description","imagepath" i can show it, but the data from the replys object doesn't show

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

Code

  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" is an array. Try this: name["replays"][0]["replay"].toString()

By adding [0] it will get your first object from that array.

EDIT: After looking at your json some more I see that name is the wrong object. "replays" is a member of "doc" not of "image" .

I think this should work:

replay = value['doc']["replays"][0]["replay"].toString();

The problem is "replays": [ { "replay": "Good\n", "Report_Date": "1590796800", "_id": "5ed248e0c1a47a3e8c4ce8bb" } ] This is a List of Maps. So as we access the first element in the list you should use

replay= value['doc']["replays"][0]["replay"].toString();

that is the zeroth element of the list.

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