我试图在Flutter中创建一个查询,该查询返回值流。查询的输入是我循环访问的documentIds
的列表。问题是流仅返回列表的第一个值。
下面是我的代码
数据库:
//Get List of wood product
List<WoodProduct> _woodProductDataFromSnapShot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return WoodProduct(
uid: doc.documentID,
productName: doc.data['productName'],
productBrand: doc.data['productBrand'],
productType: doc.data['productType'],
length: doc.data['length'],
width: doc.data['width'],
thickness: doc.data['thickness'],
color: doc.data['color'],
imageUrl: doc.data['imageUrl']);
}).toList();
}
//Stream data from wood collection reference to its id
Stream<List<WoodProduct>> woodProductIdList(List<String> uid) {
var woodProductList;
for(var id in uid)
woodProductList = woodCollection
.where(FieldPath.documentId, isEqualTo: id)
.snapshots().map(_woodProductDataFromSnapShot);
return woodProductList;
}
StreamProvider
这是我调用数据库的地方
class _CartState extends State<Cart> {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
StreamProvider<List<Products>>.value(value: null),
StreamProvider<List<WoodProduct>>.value(
value: DatabaseService().woodProductIdList(widget.cartListWood),
)
],
child: Scaffold(
appBar: AppBar(
title: Text(CART_TITLE),
backgroundColor: Colors.amber[400],
elevation: 0.0,
),
body: Container(
child: CartGridForm(woodListItem: widget.cartListWood),
),
),
);
}
}
class CartGridForm extends StatefulWidget {
final List<String> woodListItem;
CartGridForm({this.woodListItem});
@override
_CartGridFormState createState() => _CartGridFormState();
}
class _CartGridFormState extends State<CartGridForm> {
List<String> woodListItems;
@override
void initState() {
woodListItems = widget.woodListItem;
super.initState();
}
@override
Widget build(BuildContext context) {
//final paint = Provider.of<List<Products>>(context) ?? [];
final wood = Provider.of<List<WoodProduct>>(context) ?? [];
return ListView.builder(
padding: EdgeInsets.all(8.0),
itemCount: wood.length,
itemBuilder: (context, index) {
print(wood.length);
return Container(
alignment: Alignment.center,
child: selectedProdcuts(context, paint: null, wood: wood[index]),
);
});
}
Widget selectedProdcuts(BuildContext context,
{List<Products> paint, WoodProduct wood}) {
return Container(
width: 100.0,
height: 100.0,
child: Text(wood.productName),
);
}
}
我需要找到一种方法来返回列表中的所有值,而不仅仅是第一个。而且,还有没有更好的方法可以遍历where子句的ID列表?