动态添加维度到布尔数组

Dynamically add dimensions to boolean array

我正在使用 flutter 开发电子商务应用程序。
我正在为此开发 navDrawer,我可以在类别方面使用一些帮助。
我的类别可以有子类别,子类别也可以有自己的子类别。
基本上,数据集是一个未知维度的数组。

我需要为我的类别和子类别创建一个布尔映射,以便我可以跟踪哪些是打开的以便显示子类别。

这是数据集的示例:

{
      "id":"41490",
      "name":"Electrical Equipment",
      "subCategories":[
         {
            "id":"41492",
            "name":"Breakers",
            "subCategories":[
               {
                  "id":"167542",
                  "name":"1 Pole",
                  "subCategories":[
                     {
                        "id":"167577",
                        "name":"15 Amp",
                        "subCategories":null
                     },
                     {
                        "id":"167585",
                        "name":"20 Amp",
                        "subCategories":null
                     },
                     {
                        "id":"167600",
                        "name":"30 Amp",
                        "subCategories":null
                     },
                     {
                        "id":"167606",
                        "name":"40 Amp",
                        "subCategories":null
                     }
                  ]
               },

我认为递归是处理此数据集的最佳方式,但我遇到的问题是我无法弄清楚如何在 Dart 中为数组设置动态维度。
我已经想出了如何从数据集中生成我的 listTiles,但我无法弄清楚布尔映射。
这甚至可能还是我应该研究不同的方法?

这是我从数据集生成 listTiles 的代码:

  void setCategories(List categories){
      _categories = categories;
      int catCount = categories.length;
      _categoryList = new ListView.builder(
          //shrinkWrap: true,
          //physics: ClampingScrollPhysics(),
          padding:EdgeInsets.all(0.0),
          itemCount: catCount,
          itemBuilder: (BuildContext context, int index) => buildCategories(context, index),
      );
  }
  Widget buildCategories(BuildContext context, int index){
    if(_categories[index]['subCategories']!=null){
      //TODO: call buildSubCategories with depth of 1 parameter
      return Container(
          height: 30.0,
          child: ListTile(
              title: Row(
                children:[
                  Text("        "+_categories[index]['name']),
                  Transform.scale(
                      scale: 0.75,
                      child:
                      Icon(Icons.arrow_back)
                  )
                ]
              ),
              onTap: () {
                  //TODO: implement boolean map here
              }
          ),
          padding: EdgeInsets.all(0.0),
          margin: EdgeInsets.all(0.0)
      );
    } else {
      return Container(
          height: 30.0,
          child: ListTile(
              title: Text("        "+_categories[index]['name']),
              onTap: () {

              }
          ),
          padding: EdgeInsets.all(0.0),
          margin: EdgeInsets.all(0.0)
      );
    }
  }

  Widget buildSubCategories(var parent, int depth){
      List subCategoryList = parent['subCategories'];
      int subCategoryCount = subCategoryList.length;
      if(parent['subCategories']!=null){
          //for each subCategory
          //if subCategory has subCategories
          //recurse subCategory with depth

          buildSubCategories(parent['subCategories'], depth++);
          //TODO: implement boolean map here
      } else {
          //
      }
  }

  void generateCategoryBooleanMap(){
      //TODO: generate boolean map here
      //TODO: boolean map needs to have a undetermined amount of depth levels
  }

任何见解都值得赞赏,即使这意味着我必须使用不同的范例。

使用 Set 跟踪哪个 id 处于打开状态的示例:

void main() {
  final idHandler = IdHandler();

  print(idHandler.isIdOpen('MyId')); // false
  idHandler.openId('MyId');
  
  print(idHandler.isIdOpen('MyId')); // true
  idHandler.closeId('MyId');
  
  print(idHandler.isIdOpen('MyId')); // false
  idHandler.openId('MyId');
  
  print(idHandler.isIdOpen('MyId')); // true
  idHandler.closeAll();
  
  print(idHandler.isIdOpen('MyId')); // false
}

class IdHandler {
  final Set<String> _openIds = {};

  void openId(String id) => _openIds.add(id);
  void closeId(String id) => _openIds.remove(id);
  void closeAll() => _openIds.clear();
  bool isIdOpen(String id) => _openIds.contains(id);
}