简体   繁体   中英

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

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

How can i get that. Please help.

{ "sno": 69, "userid": "ram@gmail.com", "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": "newdirector@mailinator.com", "status": "Y", "disName": "prathap s", "disUrl": null, "createdBy": "newdirector@mailinator.com", "createdDate": "2022-07-04T04:54:01.000+0000", "updatedDate": "2022-07-04T04:54:01.000+0000" }, { "sno": 478, "nfid": 69, "userid": "sriram@gmail.com", "status": "Y", "disName": "sr i Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "sriram@gmail.com", "createdDate": "2022-07-04T05:02:13.000+0000", "updatedDate": "2022-07-04T05:02:13.000+0000" }, { "sno": 479, "nfid": 69, "userid": "sriram@gmail.com", "status": "Y", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "sriram@gmail.com", "createdDate": "2022-07-04T05:02:13.000+0000", "updatedDate": "2022-07-04T05:02:13.000+0000" }, { "sno": 480, "nfid": 69, "userid": "sriram@gmail.com", "status": "D", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "sriram@gmail.com", "createdDate": "2022-07-04T05:02:36.000+0000", "updatedDate": "2022-07-04T05:21:43.000+0000" }, { "sno": 481, "nfid": 69, "userid": "sriram@gmail.com", "status": "Y", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png" , "createdBy": "sriram@gmail.com", "createdDate": "2022-07-04T05:02:36.000+0000", "updatedDate": "2022-07-04T05:02:36.000+0000" }, { "sno": 499, "nfid": 69, "userid": "rajesh@gmail.com", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "rajesh@gmail.com", "createdDate": "2022-07-04T05:48:57.000+0000", "updatedDate": "2022-07-04T05:48:57.000+0000" }, { "sno": 500, "nfid": 69, "userid": "rajesh@gmail.com", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "rajesh@gmail.com", "createdDate": "2022-07-04T05:48:57.000+0000", "updatedDate": "2022-07-04T05:48:57.000+0000" }, { "sno": 501, "nfid": 69, "userid": "rajesh@gmail.com", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "rajesh@gmail.com", "createdDate": "2022-07-04T05:48:57.000+0000", "updatedDate": "2022-07-04T05:48:57.000+0000" } ] },

Firstly your JSON is wrong try with this json.

 { "sno": 69, "userid": "ram@gmail.com", "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": "newdirector@mailinator.com", "status": "Y", "disName": "prathap s", "disUrl": null, "createdBy": "newdirector@mailinator.com", "createdDate": "2022-07-04T04:54:01.000+0000", "updatedDate": "2022-07-04T04:54:01.000+0000" }, { "sno": 478, "nfid": 69, "userid": "sriram@gmail.com", "status": "Y", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "sriram@gmail.com", "createdDate": "2022-07-04T05:02:13.000+0000", "updatedDate": "2022-07-04T05:02:13.000+0000" }, { "sno": 481, "nfid": 69, "userid": "sriram@gmail.com", "status": "Y", "disName": "sri Ram", "disUrl": "https://uat-marc.s3.ap-south-1.amazonaws.com/profile-images/1284.png", "createdBy": "sriram@gmail.com", "createdDate": "2022-07-04T05:02:36.000+0000", "updatedDate": "2022-07-04T05:02:36.000+0000" }, { "sno": 499, "nfid": 69, "userid": "rajesh@gmail.com", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "rajesh@gmail.com", "createdDate": "2022-07-04T05:48:57.000+0000", "updatedDate": "2022-07-04T05:48:57.000+0000" }, { "sno": 500, "nfid": 69, "userid": "rajesh@gmail.com", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "rajesh@gmail.com", "createdDate": "2022-07-04T05:48:57.000+0000", "updatedDate": "2022-07-04T05:48:57.000+0000" }, { "sno": 501, "nfid": 69, "userid": "rajesh@gmail.com", "status": "Y", "disName": "rajesh kumar", "disUrl": null, "createdBy": "rajesh@gmail.com", "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 .

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.

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