2

Creating tabs with types and need to display in Corresponding TabView..

 var listOfItem = [
    {
      "types": "ARABIAN",
      "restaurants": [
        {
          "name": "ZauZau Restaurants",
          "location": "Mascat, Oman",
          "kms": "1.0 kms"
        },
        {
          "name": "MauMau Restaurants",
          "location": "Calicut, Oman",
          "kms": "2.0 kms"
        },
        {
          "name": "ChaCha Restaurants",
          "location": "Charat, Oman",
          "kms": "2.5 kms"
        },
      ]
    },
    {
      "types": "COFFEE SHOP",
      "restaurants": [
        {
          "name": "ZauZau Restaurants",
          "location": "Mascat, Oman",
          "kms": "1.0 kms"
        },
      ]
    },
  ];

This is my list of objects I am accessing its "restaurants" by

listOfItem.asMap().entries.map((e) => print(e.value['restaurants'])).toList();

And I need to map that particular List Trying long time. I didn't get any idea on this BTW I am new to coding..

2 Answers 2

2

Quick answer

You can nest two mapping of your JSON data:

listOfItem.map((category) => Column(
  children: (category['restaurants'] as List<Map<String, dynamic>>)
      .map((restaurant) => Text(restaurant['name'])).toList()
).toList();

Remember to use toList() as a map operation does not return a List but an Iterable.

Basic Full Solution

Here is a first solution based on your data and the Flutter Tabs.

enter image description here

Full source code:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: HomePage(),
    ),
  );
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: data.length,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Restaurants'),
          bottom: TabBar(
            tabs: data.map((category) => Tab(text: category['types'])).toList(),
          ),
        ),
        body: TabBarView(
          children: data
              .map((category) => ListView(
                    children:
                        (category['restaurants'] as List<Map<String, dynamic>>)
                            .map(
                              (restaurant) => ListTile(
                                title: Text(restaurant['name']),
                                subtitle: Text(restaurant['location']),
                              ),
                            )
                            .toList(),
                  ))
              .toList(),
        ),
      ),
    );
  }
}

final data = [
  {
    "types": "ARABIAN",
    "restaurants": [
      {
        "name": "ZauZau Restaurants",
        "location": "Mascat, Oman",
        "kms": "1.0 kms"
      },
      {
        "name": "MauMau Restaurants",
        "location": "Calicut, Oman",
        "kms": "2.0 kms"
      },
      {
        "name": "ChaCha Restaurants",
        "location": "Charat, Oman",
        "kms": "2.5 kms"
      },
    ]
  },
  {
    "types": "COFFEE SHOP",
    "restaurants": [
      {
        "name": "ZauZau Restaurants",
        "location": "Mascat, Oman",
        "kms": "1.0 kms"
      },
    ]
  },
];

Advanced Full Solution

And here is a second solution providing exactly the same visual but managing specific Domain Entities Restaurant & RestaurantCategory.

It introduces concepts such as State Management and JSON Serialization.

Full source code:

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

part '66318447.tabs.freezed.dart';
part '66318447.tabs.g.dart';

void main() {
  runApp(
    ProviderScope(
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Demo',
        home: HomePage(),
      ),
    ),
  );
}

class HomePage extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final data = useProvider(dataProvider).state;
    return DefaultTabController(
      length: data.length,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Restaurants'),
          bottom: TabBar(
            tabs: data.map((category) => Tab(text: category.name)).toList(),
          ),
        ),
        body: TabBarView(
          children: data
              .map((category) => ListView(
                    children: category.restaurants
                        .map(
                          (restaurant) => ListTile(
                            title: Text(restaurant.name),
                            subtitle: Text(
                                '${restaurant.location} (${restaurant.kms} away)'),
                          ),
                        )
                        .toList(),
                  ))
              .toList(),
        ),
      ),
    );
  }
}

final dataProvider = StateProvider<List<RestaurantCategory>>(
    (ref) => data.map((json) => RestaurantCategory.fromJson(json)).toList());

@freezed
abstract class Restaurant with _$Restaurant {
  const factory Restaurant(String name, String location, String kms) =
      _Restaurant;

  factory Restaurant.fromJson(Map<String, dynamic> json) =>
      _$RestaurantFromJson(json);
}

@freezed
abstract class RestaurantCategory with _$RestaurantCategory {
  const factory RestaurantCategory(String name, List<Restaurant> restaurants) =
      _RestaurantCategory;

  factory RestaurantCategory.fromJson(Map<String, dynamic> json) =>
      _$RestaurantCategoryFromJson(json);
}

final data = [
  {
    "name": "ARABIAN",
    "restaurants": [
      {
        "name": "ZauZau Restaurants",
        "location": "Mascat, Oman",
        "kms": "1.0 kms"
      },
      {
        "name": "MauMau Restaurants",
        "location": "Calicut, Oman",
        "kms": "2.0 kms"
      },
      {
        "name": "ChaCha Restaurants",
        "location": "Charat, Oman",
        "kms": "2.5 kms"
      },
    ]
  },
  {
    "name": "COFFEE SHOP",
    "restaurants": [
      {
        "name": "ZauZau Restaurants",
        "location": "Mascat, Oman",
        "kms": "1.0 kms"
      },
    ]
  },
];
Sign up to request clarification or add additional context in comments.

Comments

1

Try this loop:

for (final x in listOfItem){
    //x is the current object here
    for (final y in x["restaurants"]){
        //y is the current restaurant
        print(y["property you want"]);
    }
}

3 Comments

So then, you can use that similar loop to loop in the restaurants and retrieve the properties: for (final x in listOfItem){ for (final y in x["restaurants"]){ print(y); } }
I see that my code in comment is not readable very well. So I will edit answer.
Thank you, yes the solution is nested loops. If you solve the problem, can you please mark my answer as solution?

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.