如何解决Flutter分组的ListView
我有一个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 举报,一经查实,本站将立刻删除。