Firebase 多选择查询firestore文档
我只需要根据多选项筛选和查询在列表平铺列表中选择的firestore文档 我当前的选择是,如果所选选项上的查询函数等于Firestore中的可用数据,则使用query withFirebase 多选择查询firestore文档,firebase,flutter,Firebase,Flutter,我只需要根据多选项筛选和查询在列表平铺列表中选择的firestore文档 我当前的选择是,如果所选选项上的查询函数等于Firestore中的可用数据,则使用query with getFoods(FoodNotifier foodNotifier) async { var query = Firestore.instance .collection('Foods') .orderBy('create at', descending: true); if
getFoods(FoodNotifier foodNotifier) async {
var query = Firestore.instance
.collection('Foods')
.orderBy('create at', descending: true);
if (food.newcategory != widget.favoriteCategory[index].isSelected) {
query = query.where("name", isEqualTo: food.name);
}
QuerySnapshot snapshot = await query.getDocuments();
List<Food> _foodList = [];
snapshot.documents.forEach((document) {
Food food = Food.fromMap(document.data);
_foodList.add(food);
});
foodNotifier.foodList = _foodList;
}
getFoods(FoodNotifier FoodNotifier)异步{
var query=Firestore.instance
.收集(“食品”)
.orderBy('create at',降序:true);
if(food.newcategory!=widget.favoriteCegory[index].isSelected){
query=query.where(“name”,isEqualTo:food.name);
}
QuerySnapshot snapshot=wait query.getDocuments();
列表_foodList=[];
snapshot.documents.forEach((文档){
Food=Food.fromMap(document.data);
_食物列表。添加(食物);
});
foodNotifier.foodList=\u foodList;
}
我想根据下面的类中提到的一个特定类别筛选列表视图中的数据,但它不能正常工作
class FoodChoice{
FoodChoice(this.newcategory);
String newcategory;
bool isSelected=false;
FoodChoice.fromMap(Map<String, dynamic> data) {
newcategory = data['newcategory'];
}
}
class食品选择{
FoodChoice(这个新类别);
字符串新类别;
bool-isSelected=false;
FoodChoice.fromMap(地图数据){
newcategory=数据['newcategory'];
}
}
列出收藏夹类别;
选择(此。收藏类别);
@凌驾
_SelectionState createState()=>\u SelectionState();
}
类_SelectionState扩展状态{
@凌驾
小部件构建(构建上下文){
返回ListView.builder(
itemBuilder:(ctx,索引){
返回手势检测器(
行为:HitTestBehavior.不透明,
onTap:(){
widget.FavoriteCography[index].isSelected=!widget.FavoriteCography[index].isSelected;
setState((){});
},
子:容器(
颜色:widget.FavoriteCography[index].isSelected
?颜色。绿色[100]
:null,
孩子:排(
儿童:[
复选框(
值:widget.FavoriteCography[index].isSelected,
一旦更改:(s){
widget.FavoriteCography[index].isSelected=!widget.FavoriteCography[index].isSelected;
setState((){});
}),
文本(widget.favoriteCography[index].newcategory)
],
),
),
);
},
itemCount:widget.FavoriteCography.length,
);
}
}
第二个选项是将选择/过滤器直接集成到StreamBuilder中,而StreamBuilder也不起作用
class Feed extends StatefulWidget {
@override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
final Set<BuildContext> _saved = new Set<BuildContext>();
@override
void initState() {
FoodNotifier foodNotifier = Provider.of<FoodNotifier>(context, listen: false);
getFoods(foodNotifier);
super.initState();
}
@override
Widget build(BuildContext context, {Widget leading, IconData trialing: Icons.keyboard_arrow_right}) {
FoodNotifier foodNotifier = Provider.of<FoodNotifier>(context);
final bool alreadySaved = _saved.contains(context);
final List<FoodChoice> favoriteCategory;
final int index;
// Future<void> _refreshList() async {
// getFoods(foodNotifier);
// }
print("building Feed");
return Scaffold(
body:StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("Products").where('newcategory',isEqualTo: favoriteCategory[index]['product_id'].snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData
? Center(child: CircularProgressIndicator())
: ListView.separated(
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: Image.network(
foodNotifier.foodList[index].image != null
? foodNotifier.foodList[index].image
: 'https://www.testingxperts.com/wp-content/uploads/2019/02/placeholder-img.jpg',
width: 120,
fit: BoxFit.fitWidth,
),
title: Text(foodNotifier.foodList[index].name),
subtitle: Text(foodNotifier.foodList[index].category),
trailing: new Icon( // Add the lines from here...
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
),
onTap: () async {
await new Future.delayed(const Duration(seconds: 1));
foodNotifier.currentFood = foodNotifier.foodList[index];
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {
return FoodDetaill();
}));
},
);
},
itemCount: foodNotifier.foodList.length,
separatorBuilder: (BuildContext context, int index) {
return Divider(
color: Colors.black,
);
},
);
}
),
);
}
}
类提要扩展StatefulWidget{
@凌驾
_FeedState createState();
}
类_FeedState扩展状态{
最终集_saved=新集();
@凌驾
void initState(){
FoodNotifier FoodNotifier=Provider.of(上下文,侦听:false);
getFoods(食品通知者);
super.initState();
}
@凌驾
小部件构建(BuildContext上下文,{Widget-leading,IconData-trialing:Icons.keyboard\u-arrow\u-right}){
FoodNotifier FoodNotifier=Provider.of(上下文);
final bool alreadySaved=\u saved.contains(上下文);
最终名单类别;
最终整数指数;
//Future\u refreshList()异步{
//getFoods(食品通知者);
// }
打印(“建筑提要”);
返回脚手架(
正文:StreamBuilder(
stream:Firestore.instance.collection(“产品”).where('newcategory',isEqualTo:FavoriteCography[index]['product_id'])。snapshots(),
生成器:(上下文,快照){
return!snapshot.hasData
?中心(子项:循环压缩机指示器())
:ListView.separated(
itemBuilder:(构建上下文,int索引){
返回列表块(
领先:Image.net(
foodNotifier.foodList[index].image!=null
?foodNotifier.foodList[索引].image
: 'https://www.testingxperts.com/wp-content/uploads/2019/02/placeholder-img.jpg',
宽度:120,
适合:BoxFit.fitWidth,
),
标题:文本(foodNotifier.foodList[index].name),
字幕:文本(foodNotifier.foodList[index].category),
尾随:新图标(//从此处添加行。。。
alreadySaved?Icons.favorite:Icons.favorite_边框,
颜色:已保存?颜色。红色:空,
),
onTap:()异步{
等待新的未来。延迟(持续时间(秒:1));
foodNotifier.currentFood=foodNotifier.foodList[索引];
Navigator.of(context).push(MaterialPageRoute(builder:(BuildContext){
返回FoodDetaill();
}));
},
);
},
itemCount:foodNotifier.foodList.length,
separatorBuilder:(BuildContext,int索引){
回流分配器(
颜色:颜色,黑色,
);
},
);
}
),
);
}
}
根据用户输入选择查询的最佳方法是什么?我们可以集成一个变量吗?我很难看到缺少什么,或者在列表选择和查询之间建立链接
class Feed extends StatefulWidget {
@override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
final Set<BuildContext> _saved = new Set<BuildContext>();
@override
void initState() {
FoodNotifier foodNotifier = Provider.of<FoodNotifier>(context, listen: false);
getFoods(foodNotifier);
super.initState();
}
@override
Widget build(BuildContext context, {Widget leading, IconData trialing: Icons.keyboard_arrow_right}) {
FoodNotifier foodNotifier = Provider.of<FoodNotifier>(context);
final bool alreadySaved = _saved.contains(context);
final List<FoodChoice> favoriteCategory;
final int index;
// Future<void> _refreshList() async {
// getFoods(foodNotifier);
// }
print("building Feed");
return Scaffold(
body:StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("Products").where('newcategory',isEqualTo: favoriteCategory[index]['product_id'].snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData
? Center(child: CircularProgressIndicator())
: ListView.separated(
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: Image.network(
foodNotifier.foodList[index].image != null
? foodNotifier.foodList[index].image
: 'https://www.testingxperts.com/wp-content/uploads/2019/02/placeholder-img.jpg',
width: 120,
fit: BoxFit.fitWidth,
),
title: Text(foodNotifier.foodList[index].name),
subtitle: Text(foodNotifier.foodList[index].category),
trailing: new Icon( // Add the lines from here...
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
),
onTap: () async {
await new Future.delayed(const Duration(seconds: 1));
foodNotifier.currentFood = foodNotifier.foodList[index];
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {
return FoodDetaill();
}));
},
);
},
itemCount: foodNotifier.foodList.length,
separatorBuilder: (BuildContext context, int index) {
return Divider(
color: Colors.black,
);
},
);
}
),
);
}
}