简体   繁体   English

无法访问嵌套数组 object flutter

[英]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我有一组数据,有一些细节,当我尝试显示一个值返回 null,其他 2 个数据很好,但是如果我尝试显示其他数据,它显示 null,如果我尝试将其添加到 setState,一切变成null,当我得到“描述”,“图像路径”时没有问题我可以显示它,但是来自回复object的数据没有显示

JSON 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. "replays"是一个数组。 Try this: name["replays"][0]["replay"].toString()试试这个: name["replays"][0]["replay"].toString()

By adding [0] it will get your first object from that array.通过添加[0] ,它将从该数组中获得您的第一个 object。

EDIT: After looking at your json some more I see that name is the wrong object.编辑:在查看了您的 json 之后,我发现该name是错误的 object。 "replays" is a member of "doc" not of "image" . "replays""doc"的成员,而不是"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.问题是"replays": [ { "replay": "Good\n", "Report_Date": "1590796800", "_id": "5ed248e0c1a47a3e8c4ce8bb" } ]这是地图列表。 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.这是列表的第零个元素。

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

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