0

The below code is decoding the Json Data for which the console output is also shown and the json data is also shown. I am able to decode the data successfully as per the console output. I want the decoded json data to be stored in an array so that I can store it in firestore document as an array.

How should I do that? I know how to store data in firestore database, all I need to store the json data in an array. Please guide me

CODE

 var parsedData = json.decode(state.successResponse);
                List members = parsedData['members'];
                 members.forEach((member){
               
                  String name1 = member['firstName'];
                 print(name1);
       });

JSON DATA

flutter: {
    "members": [
        {
            "firstName": "Michael"
        },
        {
            "firstName": "Jennifer"
        },
        {
            "firstName": "Lisa"
        }
    ]
}

CONSOLE OUTPUT

flutter:Michael
flutter:Jennifer
flutter:Lisa
0

1 Answer 1

1

ANSWER 2.0

If the case is to store the data into the array, then we need to have a List<String> which will store the firstname param. You can then use _firstNames list to be passed to your firestore

    // This will store the name from the data
    List<String> _firstNames = [];
    var parsedData = json.decode(state.successResponse);
    List members = parsedData['members'];
    members.forEach((member){
         // simply add it to the list the data
         _firstNames.add(member['firstName']);
    });

    // print it to check, whether you have got it or not, 
    // outside the loop
    print(_firstNames);

I will give out demo as per your data set only in this code example

  List<String> _firstNames = [];
  Map _membersData = {
    "members": [
        {
            "firstName": "Michael"
        },
        {
            "firstName": "Jennifer"
        },
        {
            "firstName": "Lisa"
        }
    ]
  };
  
  _membersData["members"].forEach((data){
    _firstNames.add(data["firstName"]);
  });
  print(_firstNames);

  // OUTPUT will be [Michael, Jennifer, Lisa]

Try it and let me know

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

7 Comments

Alok, I need to store the data in firestore database, the output you shared I can already generate that. In firestore database document I need to store multiple fields of same kind just like we refer array in java or swift or any language, I need to convert the firstName output to array so that I can save that in firestore database. I hope you have idea of firestore database.
Can you show me how your output should look like, don't code, just show? I will help you out on that @JohnnyWarner. Just this favor is required from your side.
I the data JsonData and Console Output I shared, are the types of outputs I am I able to generate. I need to convert that in to an array of Strings so that I can store it in firestore database
So the array would be like this: ['Michael', 'Jennifer', 'Lisa'] @JohnnyWarner?
@JohnnyWarner answer updated, please see, and let me know
|

Your Answer

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