简体   繁体   English

Cloud Firestore Firebase Collection ref 上的 IN 查询是否有 Flutter/Dart 等价物?

[英]Is there a Flutter/Dart equivalent for the IN query on a Cloud Firestore Firebase Collection ref?

I want to query a Firebase collection reference with a list of multiple document IDs within a Streambuilder, but it appears the only我想查询 Firebase 集合引用,其中包含 Streambuilder 中的多个文档 ID 列表,但它似乎是唯一的

.where()

query functions available are:可用的查询功能有:

Query where(String field, {dynamic isEqualTo, dynamic isLessThan, dynamic isLessThanOrEqualTo, dynamic isGreaterThan, dynamic isGreaterThanOrEqualTo, dynamic arrayContains, bool isNull})

I have tested isEqualTo and this works fine if I set a string to pass in, but I can't get this to work with a list.我已经测试过 isEqualTo,如果我设置要传入的字符串,它就可以正常工作,但我无法让它与列表一起使用。 It appears there is an IN function in the official Firebase documentation which would provide this function, to a maximum of 10 items:在官方 Firebase 文档中似乎有一个 IN function,它将提供这个 function,最多 10 个项目:

https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any

Is there an equivalent IN function in Flutter or another way to achieve this? Flutter 中是否有等效的 IN function 或其他实现方法? The list I am trying to pass in is actually served by a parent Streambuilder, which queries a User collection for the specific user and passes in all groups to the second Streambuilder:我试图传递的列表实际上是由父 Streambuilder 提供的,它查询特定用户的用户集合并将所有组传递给第二个 Streambuilder:

class _StarredChatState extends State<StarredChat> {

final _firestore = Firestore.instance;
  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    final userID = user.uid;   
    return StreamBuilder<QuerySnapshot>( 
      stream: _firestore.collection('users/$userID/chats')
                        .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }
        List<String> ChatIDList = [];
        final ChatIDs = snapshot.data.documents;
        for(var x in ChatIDs) {
          final ChatID = x.data['chats'].toString();
          ChatIDList.add(ChatID);
        }

        return StreamBuilder<QuerySnapshot>(
          stream: _firestore.collection('chatrooms')
                            // here is where I need the IN operator as neither
                            // arraycontains or isEqualTo give the desired result
                            .where('description', arrayContains: ChatIDList)
                            .snapshots(),
          builder: (context, snapshot) {

          if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }
          //Here is where I want to return a ListBuilder for the above returned 
          //Streambuilder list

          }

          );


      }
    );
  }
}

My other attempt has been to include all users who have joined a chatroom as a subcollection within the individual chat document, but as these have unique autoID's I'm not able to generate a stream of all chats with my specific user as I can't pass in a full path to the nested subcollection.我的另一个尝试是将所有加入聊天室的用户作为子集合包含在单个聊天文档中,但由于这些用户具有唯一的自动 ID,因此我无法生成与我的特定用户的所有聊天的 stream,因为我不能传入嵌套子集合的完整路径。 Any other pointers would be really appreciated.任何其他指针将不胜感激。

Thanks谢谢

My guess is that you're on an older version of FlutterFire's cloud_firestore plugin, as the whereIn operator is definitely present in the current version of the library.我的猜测是您使用的是旧版本的 FlutterFire 的cloud_firestore插件,因为whereIn运算符肯定存在于当前版本的库中。 See the reference docs here .请参阅此处的参考文档。

You might want to upgrade to the latest version of FlutterFire, to get the operator.你可能想升级到最新版本的 FlutterFire,以获取操作员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM