【发布时间】:2021-11-27 11:13:32
【问题描述】:
我正在尝试获取集合中具有等于特定字符串的特定字段的文档。我正在构建一个 POS,我想获得特定城市的所有销售额。
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final CollectionReference _mainCollection = _firestore.collection('Sales');
Stream<QuerySnapshot> readFeeds() {
CollectionReference notesItemCollection =
_mainCollection.where('seller_location', isEqualTo: "London").get();
return notesItemCollection.snapshots();
}
我收到此错误:
“Future
>”类型的值不能分配给“CollectionReference
我已经添加了演员as CollectionReference<Object?>;,但查询仍然无法正常工作。这就是我访问数据的方式:
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: readFeeds(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
} else if (snapshot.hasData || snapshot.data != null) {
return ListView.separated(
separatorBuilder: (context, index) => SizedBox(height: 16.0),
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
var noteInfo = snapshot.data!.docs[index];
String docID = snapshot.data!.docs[index].id;
String name = noteInfo['name'].toString();
String price = noteInfo['price'].toString();
String quantity = noteInfo['quantity'].toString();
return Ink(
decoration: BoxDecoration(
color: CustomColors.firebaseGrey.withOpacity(0.1),
borderRadius: BorderRadius.circular(8.0),
),
child: ListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => EditScreen(
documentId: docID,
currentName: name,
currentPrice: price,
currentQuantity: quantity,
),
),
),
title: Text(
name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: Colors.white),
),
),
);
},
);
}
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
CustomColors.firebaseOrange,
),
),
);
},
);
}
}
【问题讨论】:
标签: firebase flutter dart google-cloud-platform google-cloud-firestore