Home > database >  How to get the length of array inside the list of array from api response - flutter
How to get the length of array inside the list of array from api response - flutter

Time:07-04

This is the api response. I have to get the length of "newslikes" length?

How can i get that. Please help.

{ "sno": 69, "userid": "[email protected]", "postUrl": "[https://uat-marc.s3.ap-south-1.amazonaws.com/news-feed/69_0.jpg@ https://uat-marc.s3.ap-south-1.amazonaws.com/news-feed/69_1.jpg@ https://uat-marc.s3.ap-south-1.amazonaws.com/news-feed/69_2.jpg]", "contentType": "img", "descrip": "jpg or jpeg 3flrs", "dispname": "Ram", "city": "4", "chamberName": "null", "keydesc": "null", "profilepic": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1274.jpg", "createdBy": "null", "postStatus": "Y", "createdDate": "2022-07-03T11:52:49.000 0000", "newsLikes": [ { "sno": 472, "nfid": 69, "userid": "[email protected]", "status": "Y", "disName": "prathap s", "disUrl": null, "createdBy": "[email protected]", "createdDate": "2022-07-04T04:54:01.000 0000", "updatedDate": "2022-07-04T04:54:01.000 0000" }, { "sno": 478, "nfid": 69, "userid": "[email protected]", "status": "Y", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "[email protected]", "createdDate": "2022-07-04T05:02:13.000 0000", "updatedDate": "2022-07-04T05:02:13.000 0000" }, { "sno": 479, "nfid": 69, "userid": "[email protected]", "status": "Y", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "[email protected]", "createdDate": "2022-07-04T05:02:13.000 0000", "updatedDate": "2022-07-04T05:02:13.000 0000" }, { "sno": 480, "nfid": 69, "userid": "[email protected]", "status": "D", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "[email protected]", "createdDate": "2022-07-04T05:02:36.000 0000", "updatedDate": "2022-07-04T05:21:43.000 0000" }, { "sno": 481, "nfid": 69, "userid": "[email protected]", "status": "Y", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "[email protected]", "createdDate": "2022-07-04T05:02:36.000 0000", "updatedDate": "2022-07-04T05:02:36.000 0000" }, { "sno": 499, "nfid": 69, "userid": "[email protected]", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "[email protected]", "createdDate": "2022-07-04T05:48:57.000 0000", "updatedDate": "2022-07-04T05:48:57.000 0000" }, { "sno": 500, "nfid": 69, "userid": "[email protected]", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "[email protected]", "createdDate": "2022-07-04T05:48:57.000 0000", "updatedDate": "2022-07-04T05:48:57.000 0000" }, { "sno": 501, "nfid": 69, "userid": "[email protected]", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "[email protected]", "createdDate": "2022-07-04T05:48:57.000 0000", "updatedDate": "2022-07-04T05:48:57.000 0000" } ] },

CodePudding user response:

Firstly your JSON is wrong try with this json.

{
  "sno": 69,
  "userid": "[email protected]",
  "postUrl": [
    "https://uat-marc.s3.ap-south-1.amazonaws.com/news-feed/69_0.jpg",
    " https://uat-marc.s3.ap-south-1.amazonaws.com/news-feed/69_1.jpg",
    " https://uat-marc.s3.ap-south-1.amazonaws.com/news-feed/69_2.jpg"
  ],
  "contentType": "img",
  "descrip": "jpg or jpeg 3flrs",
  "dispname": "Ram",
  "city": "4",
  "chamberName": "null",
  "keydesc": "null",
  "profilepic": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1274.jpg",
  "createdBy": "null",
  "postStatus": "Y",
  "createdDate": "2022-07-03T11:52:49.000 0000",
  "newsLikes": [
    {
      "sno": 472,
      "nfid": 69,
      "userid": "[email protected]",
      "status": "Y",
      "disName": "prathap s",
      "disUrl": null,
      "createdBy": "[email protected]",
      "createdDate": "2022-07-04T04:54:01.000 0000",
      "updatedDate": "2022-07-04T04:54:01.000 0000"
    },
    {
      "sno": 478,
      "nfid": 69,
      "userid": "[email protected]",
      "status": "Y",
      "disName": "sri Ram",
      "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png",
      "createdBy": "[email protected]",
      "createdDate": "2022-07-04T05:02:13.000 0000",
      "updatedDate": "2022-07-04T05:02:13.000 0000"
    },
    {
      "sno": 481,
      "nfid": 69,
      "userid": "[email protected]",
      "status": "Y",
      "disName": "sri Ram",
      "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png",
      "createdBy": "[email protected]",
      "createdDate": "2022-07-04T05:02:36.000 0000",
      "updatedDate": "2022-07-04T05:02:36.000 0000"
    },
    {
      "sno": 499,
      "nfid": 69,
      "userid": "[email protected]",
      "status": "Y",
      "disName": "rajesh kumar",
      "disUrl": null,
      "createdBy": "[email protected]",
      "createdDate": "2022-07-04T05:48:57.000 0000",
      "updatedDate": "2022-07-04T05:48:57.000 0000"
    },
    {
      "sno": 500,
      "nfid": 69,
      "userid": "[email protected]",
      "status": "Y",
      "disName": "rajesh kumar",
      "disUrl": null,
      "createdBy": "[email protected]",
      "createdDate": "2022-07-04T05:48:57.000 0000",
      "updatedDate": "2022-07-04T05:48:57.000 0000"
    },
    {
      "sno": 501,
      "nfid": 69,
      "userid": "[email protected]",
      "status": "Y",
      "disName": "rajesh kumar",
      "disUrl": null,
      "createdBy": "[email protected]",
      "createdDate": "2022-07-04T05:48:57.000 0000",
      "updatedDate": "2022-07-04T05:48:57.000 0000"
    }
  ]
}

try to create model with this converter https://ashamp.github.io/jsonToDartModel/

then

YourClassName.newsLikes.length

you will get length of newsLikes.

CodePudding user response:

If you have a type defined for the API response and you mapped your API response to the type you can use

data[index].newsLikes.length

Or if you have not defined type, you can use like

data[index]['newsLikes'].length

Add null check wherever it is required and hope you would have parsed the JSON from JSON string if in case you receive JSON String as the response.

  • Related