0

Background

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

My Conceptual Solution

I was thinking of stripping out the first item logged in and forming a whole new Json array with just Items 1-3

My Question

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

Alternatives

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! :)

1
  • @Pythony When I try to iterate the entire list I'm getting this error: type '(dynamic,dynamic) => Null' is not a subtype of '(dynamic) => dynamic' of 'f' Commented Jun 24, 2021 at 3:44

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this is working great! As a follow-up may I ask: How would you remove that first loggedin item and return either a list or a map with all the remaining data intact without redefining everything allover again?
Happy it helped! You can remove the first item in the list with this decoded.removeAt(0);
0

I'd recommend creating a Model class with the below fields:

  • id
  • title
  • excerpt
  • thumb
  • threadid
  • fid
  • commentcount
  • postdate
  • loggedin

Now, for the first item, all fields will be null except loggedin and for other items loggedin will be null.

Comments

0

Convert response to List<Map>, iterate through list, ignore item that contains keys loggedin

    final List<Map> list = json.decode(response.body);
    list.forEach((element) { 
      if(!element.containsKey("loggedin")){
        //Here element is map of your required item
        //You can convert it to your required model
      }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.