0

[enter image description here][1] I'm creating a maintenance app in flutter with dummy model.

what I'm try to achieve is when the user select a main category the screen should navigate to the subcategory then when the user select the subcategories the screen should navigate to the super-subcategory. I already took the main category datas to the GridView.builder

Now I want to get the subcategory iconPath and name to make a new GridView.builder

final List<Category> mianCategory = [
  Category(
    iconPath: 'assets/svg/electrical.svg',
    name: 'Electrical',
    subCategory: [
      Category(
        iconPath: 'assets/svg/plug.svg',
        name: 'Plug',
        superSubCategory: [
          'Plug Not Working',
          'Fuse Neeeds Replacement',
          'Other'
        ],
      ),
  
      Category(
        iconPath: 'assets/svg/communication.svg',
        name: 'Communication',
        superSubCategory: [
          'Plug Not Working',
          'Fuse Neeeds Replacement',
          'Other'
        ],
      ),
    ],
  ),

Here is my Model

class Category {
  final String? iconPath;
  final String name;
  final List<Category>? subCategory;
  final List<String>? superSubCategory;

  const Category({
    this.iconPath,
    required this.name,
    this.subCategory,
    this.superSubCategory,
  });
}


  [1]: https://i.stack.imgur.com/UL79U.png
Nashaf
  • 23
  • 7

1 Answers1

0

If I understood your question correctly, you can do that by recursion:

List<String> getIconPaths(List<Category> categories) {
  List<String> iconPaths = [];
  categories.forEach((e) {
    if (e.subCategory?.isNotEmpty ?? false) {
      iconPaths.addAll(getIconPaths(e.children!));
    }
    if (e.iconPath != null) iconPaths.add(e.iconPath);
  });
  return iconPaths;
}

List<String> getsuperSubCategories(List<Category> categories) {
  List<String> superSubCategories = [];
  categories.forEach((e) {
    if (e.subCategory?.isNotEmpty ?? false) {
      superSubCategories.addAll(getsuperSubCategories(e.children!));
    }
    if (e.superSubCategory != null) superSubCategories.addAll(e.superSubCategory);
  });
  return superSubCategories;
}
omar hatem
  • 1,799
  • 1
  • 10
  • 23