【问题标题】:How to pass DocumentSnapshot Id between two classes?如何在两个类之间传递 DocumentSnapshot Id?
【发布时间】:2020-12-13 08:22:56
【问题描述】:

我有两个下拉菜单。一个代表类别,其他代表子类别。这些值应从 firesostore 中检索。集合作为嵌套集合创建的位置。

Categories & SubCategories collection are shown in the image

class SelectCategory extends StatefulWidget {
  @override
  _SelectCategoryState createState() => _SelectCategoryState();
}

class _SelectCategoryState extends State<SelectCategory> {
  AdminDatabaseMethods adminDatabaseMethods = AdminDatabaseMethods();
  var selectedCategory;

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: adminDatabaseMethods.getCategories(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
          List<DropdownMenuItem> categoryMenu = [];
          for (var i = 0; i < snapshot.data.docs.length; i++) {
            DocumentSnapshot snap = snapshot.data.docs[i];
            categoryMenu.add(DropdownMenuItem(
              child: Text(snap.data()["Category_Name"]),
              value: "${snap.id}",
            ));
            //adminDatabaseMethods.getSubCategories(snap);

          }
          return DropdownButton(
            value: selectedCategory,
            icon: Icon(
              Icons.arrow_downward_sharp,
              color: Colors.amber,
            ),
            iconSize: 20,
            elevation: 16,
            hint: Text(
              "Select Main Categories",
              style: TextStyle(color: Colors.amber),
            ),
            style: TextStyle(color: Colors.amber),
            underline: Container(
              height: 2,
              color: Colors.amber[300],
            ),
            onChanged: (categoryValue) {
              setState(() {
                selectedCategory = categoryValue;
                adminDatabaseMethods.getSubCategories(selectedCategory);
              });
            },
            items: categoryMenu,
          );
        });
  }
}

这就是我创建类别列表的方式,并按照相同的方式创建子类别。我需要的是当我按下需要将文档快照 ID 传递给其他类的类别时。 哪里,

Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  SizedBox(
                    width: 15,
                  ),
                  SelectCategory(),
                  SizedBox(
                    width: 15,
                  ),
                  SelectsubCategories(),
                  SizedBox(
                    width: 15,
                  ),
                ],
              ),

类别和子类别是这样给出的。有没有办法传递数据。由于我是新手,请建议我一种方法,谢谢。

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    我认为您正在寻找的是状态管理解决方案。了解更多here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多