问题描述
我有一个Firebase数据库,其中包含2个相关集合:“产品” 和“类别” 。
我想在按类别进行分组的列表视图中显示产品。
每个产品文档中都有一个“类别”字段,即类别ID。
现在,我有以下代码可用于创建列表视图:
FullMenuNotifier fullMenuNotifier = Provider.of<FullMenuNotifier>(context);
ListView.builder(
shrinkWrap: true,scrollDirection: Axis.vertical,itemBuilder: (BuildContext context,int index) {
return Card(...);
},itemCount: fullMenuNotifier.menuList.length,);
我使用以下代码从Firebase检索数据:
getMenu(FullMenuNotifier fullMenuNotifier)async{
QuerySnapshot snapshot = await FirebaseFirestore.instance.collection('Products').get();
List<FullMenu> _fullMenuList=[];
snapshot.docs.forEach((element) {
FullMenu fullMenu = FullMenu.fromMap(element.data());
_fullMenuList.add(fullMenu);
});
fullMenuNotifier.menuList=_fullMenuList;
}
通知程序代码,
class FullMenuNotifier with ChangeNotifier{
List<FullMenu>_menuList=[];
FullMenu _currentMenu;
UnmodifiableListView<FullMenu>get
menuList=>UnmodifiableListView(_menuList);
FullMenu get currentMenu=>_currentMenu;
set menuList(List<FullMenu>menuList){
_menuList= menuList;
notifyListeners();
}
set currentProduct (FullMenu fullMenu){
_currentMenu= fullMenu;
notifyListeners();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)