How can I manipulate JSON data in dart / flutter for use in a listview

Mark

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

Pythony

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use SlidingUpPanel on a ListView in Flutter?

How can I manipulate a string in dart?

How can I use gather function to manipulate my data frame?

How can I store a Future <dynamic> data for later use within a program in dart/Flutter?

How can I correctly use If conditionals in onPressed and setState (dart, flutter)

How can i deserialize my json in Flutter/dart

How can I use the Showcase View in a ListView Item in Flutter?

How can I use a list in ListView and ListTile in Flutter

How can I manipulate data that is in a DataGridView box?

How can I show data in listView in flutter using POST METHOD

How to extract JSON child data in dart / flutter

How can I use data from POST request to manipulate the DOM using a script.js file

How can I manipulate this pandas dataframe with time series data in order to be more easier to use?

How can I manipulate image pixels in flutter on a perfomant way?

How can I retry a Future in Dart/Flutter?

how i can use Json data as an array

In Dart/Flutter, how do I use a variable from a method so I can ouput it to a text field

How do I add floatingactionbutton in my ListView in Flutter dart

how can use future in the listview flutter?

How to parse the json data and display it in Listview in Flutter?

how can I auto refresh listView in Flutter?

How can I use my data model in my Flutter app?

How can I use flutter provider to get data from Firestore?

Flutter | How can I use the data in void initState?

How can I create a plugin from my simple .dart file to use in Flutter?

How can I use the .where() query in firestore to navigate between different screens? Flutter, Dart, Firebase

How can I reach a specific data from JSON response in flutter?

How can i fetch json data without key in Flutter?

How can I use time in Java to manipulate code?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive