Firebase firestore 查询不会过滤数据,即使在 flutter 中使用 where

Firebase firestore queries are not filtering data, even when using where in flutter

我正在尝试在我的 flutter 应用程序的 Firebase firestore 查询中使用 where,但它显示集合中的所有数据而不过滤它。这是我的代码:

  Widget buildingMessages() {
    print('message room id $roomID'); //The correct id is being printed here
    var theMessages = FirebaseFirestore.instance.collection('messages');
    theMessages.where('room_id',isEqualTo: roomID).orderBy('created', descending: true);
    return StreamBuilder<QuerySnapshot>(
      stream: theMessages.snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text('Something went wrong');
        }

        return new ListView(
          children: snapshot.data.docs.map((DocumentSnapshot document) {
//....

问题出在stream: theMessages.snapshots()。您正在引用 theMessages。而且您没有使用 where 子句。用你的 where 子句扩展它。喜欢

stream: theMessages.snapshots().where(
                  'room_id',isEqualTo: roomID).orderBy('created', descending: true);

编辑:或初始化为

var theMessages = FirebaseFirestore.instance.collection('messages').
            where('room_id',isEqualTo: roomID).orderBy('created', descending: true);