我只需要根据“多项选择”来过滤和查询在“列表”图块列表中选择的Firestore文档。
我当前的选择是将查询与所选选项上的功能是否等于Firestore中的可用数据一起使用。
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;
}
我要针对下面的类中提到的一种特定类别过滤列表视图的数据,但是它无法正常工作。
class FoodChoice{
FoodChoice(this.newcategory);
String newcategory;
bool isSelected=false;
FoodChoice.fromMap(Map<String, dynamic> data) {
newcategory = data['newcategory'];
}
}
List<FoodChoice> favoriteCategory;
Selection(this.favoriteCategory);
@override
_SelectionState createState() => _SelectionState();
}
class _SelectionState extends State<Selection> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (ctx, index) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
widget.favoriteCategory[index].isSelected = !widget.favoriteCategory[index].isSelected;
setState(() {});
},
child: Container(
color: widget.favoriteCategory[index].isSelected
? Colors.green[100]
: null,
child: Row(
children: <Widget>[
Checkbox(
value: widget.favoriteCategory[index].isSelected,
onChanged: (s) {
widget.favoriteCategory[index].isSelected = !widget.favoriteCategory[index].isSelected;
setState(() {});
}),
Text(widget.favoriteCategory[index].newcategory)
],
),
),
);
},
itemCount: widget.favoriteCategory.length,
);
}
}
第二个选项是将选择/过滤器直接集成到同样无法正常工作的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,
);
},
);
}
),
);
}
}
根据用户输入选择查询的最佳方法是什么?我们可以整合变量吗?我正在努力查看缺少的内容,或者在列表选择和查询之间建立链接。