1

Flutter how can i show the array data of array.

My data looks like this

I/flutter ( 7557): {count: 1, 
records: [{
 cnic: 42501-0978008-5,
 name: abc 
 family_members: [{
  gender: F, 
  name: Dua Batool, 
  age: 8, 
  relationship: Daughter
}, 
{
  gender: M, 
  name: Shayan, 
  age: 9, 
  relationship: Son
}
]}]}

What I need to do is I need to print the name of Family members array.

My code is this

 Future<List> doSomeAsyncStuff() async {

    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'Cnic');
    print(value);

    String url = 'http://api.igiinsurance.com.pk:8888/insurance_IgiGen/insurance-api/get_company_employee.php?offset=0&limit=1&cnic=${value}' ;
    final msg = jsonEncode({"cnic": value});
    Map<String,String> headers = {'Content-Type':'application/json'};

    String token = value;
    final response = await http.get(url);

    var Data = json.decode(response.body);
    print(Data);
  }

Its printing Data value ,as I show you on the upper side of question, I need to print the Family members name. I know its stupid question but I am not able to print the name of the Familymembers array

2 Answers 2

2

You can do it like this:

Future<List> doSomeAsyncStuff() async {

    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'Cnic');
    print(value);

    String url = 'http://api.igiinsurance.com.pk:8888/insurance_IgiGen/insurance-api/get_company_employee.php?offset=0&limit=1&cnic=${value}' ;
    final msg = jsonEncode({"cnic": value});
    Map<String,String> headers = {'Content-Type':'application/json'};

    String token = value;
    final response = await http.get(url);

    var data = json.decode(response.body);
    print(data);

    var familyMembers = data["records"][0]["familyMembers"]
    print(familyMembers);
    for (var familyMember in familyMembers){ //prints the name of each family member
            var familyMemberName = familyMember["name"];
            print(familyMemberName);
  }

If you have multiple items in your records list, you can use a loop and print each familyMember:

Future<List> doSomeAsyncStuff() async {

    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'Cnic');
    print(value);

    String url = 'http://api.igiinsurance.com.pk:8888/insurance_IgiGen/insurance-api/get_company_employee.php?offset=0&limit=1&cnic=${value}' ;
    final msg = jsonEncode({"cnic": value});
    Map<String,String> headers = {'Content-Type':'application/json'};

    String token = value;
    final response = await http.get(url);

    var data = json.decode(response.body);
    print(data);
    var recordsList = data["records"];
    for (var record in recordsList){
        var familyMembers = record["familyMembers"]
        print(familyMembers);
        for (var familyMember in familyMembers){ //prints the name of each family member
            var familyMemberName = familyMember["name"];
            print(familyMemberName);
    }
  }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks you so much first one is working fine. Can you tell one more thing how can I show these name in Text widget ?
you're welcome. you can create a list inside your function and add the family members name to it. then return the list and use the items of the returned value as you Text widget value. I don't know how you want to show them but if you want to show all of them, use a ListView widget with the returned list
use FutureBuilder
yes you can also use a FutureBuilder and return a Text widget from its builder method
1

There is nothing to be feeling silly bout. We are here to learn. For answering your question, I am using forEach(). What it does is, it iterates over your data, and get the value from the array

Your code is this much only

  // mapping your records array
  Data['records'].forEach((item){
    // mapping your family_members array
    item['family_members'].forEach((nestedItem){
      print(nestedItem['name']);
    });
  });

Let us show this in your dart code method. You would get more idea

void main() {
  Map data = {
  'count': 1, 
  'records': [
    {
      'cnic': 42501-0978008-5,
      'name': 'abc' ,
      'family_members': [
        {
          'gender': 'F', 
          'name': 'Dua Batool', 
          'age': 8, 
          'relationship': 'Daughter'
        }, 
        {
          'gender': 'M', 
          'name': 'Shayan', 
          'age': 9, 
          'relationship': 'Son'
        }
      ]
    }           
  ]};
  
  // mapping your records array
  data['records'].forEach((item){
    // mapping your family_members array
    item['family_members'].forEach((nestedItem){
      print(nestedItem['name']);
    });
  });
  
}

OUTPUT

Dua Batool
Shayan

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.