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.sstatic.net/UL79U.png
1
  • it is ok to use nested list if needed Commented Dec 25, 2021 at 16:23

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I edit the question to make more clear also attached a photo i.sstatic.net/UL79U.png .
I solved problem this by using flutter Riverpod Thanks

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.