I have the following JSON data that is being remotely retrieved via an async request, that I'm trying to build a list view out of in Flutter.
As you can see the first item is 'logged in' which is totally different from all the other items
I'm having a lot of trouble in flutter when trying to build a list view out of the data (I'm a total flutter noob --> This is day 0).
[
{
"loggedin": "0"
},
{
"id": "1",
"title": "Title 1",
"excerpt": "",
"thumb": "Image 1.jpg",
"threadid": "1",
"fid": "1",
"commentcount": "1",
"postdate": 1
}, {
"id": "2",
"title": "Title 2",
"excerpt": "",
"thumb": "Image 2.jpg",
"threadid": "2",
"fid": "2",
"commentcount": "2",
"postdate": 2
}, {
"id": "3",
"title": "Title 3",
"excerpt": "",
"thumb": "Image3.jpg",
"threadid": "3",
"fid": "3",
"commentcount": "3",
"postdate": 3
}
]
I was thinking of stripping out the first item logged in and forming a whole new Json array with just Items 1-3
Is it possible to iterate through the decoded json data and form a new array?
I can successfully access an individual item in my list as follows:
_map = json.decode(response.body)[1];
However when I try to iterate through my list it fails
final decoded = json.decode(response.body) as dynamic;
decoded.forEach((key, value) {
if (key != "loggedin") {
debugPrint('hi');
}
});
If I try to iterate through just one of the items then it does work:
final decoded = json.decode(response.body)[1] as dynamic;
decoded.forEach((key, value) {
debugPrint(key+': '+value);
});
I needed to iterate through my list as a starting point to pick out the items I want to remove from my final list (basically the item with a key of "loggedin", but I'm failing miserably here
I realize the very concept of my approach is most likely flawed or tedious. If any other alternative approach to achieve my goal seems better I'm all ears! :)
The error's happening because your data is a list of maps but in the list iterator you are passing a function with two parameters, which works for a map.
You should do it like this -
final decoded = json.decode(response.body) as dynamic;
decoded.forEach((data) {
//data is a Map
});
You can access the individual keys like this - data["loggedin"]
To check whether it contains 'loggedin', you can do data.containsKey("loggedin")
For a list, the function in the forEach takes one Argument which is a list item, and for a Map it takes two, a key and its corresponding value.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments