简体   繁体   English

使用 flutter 中的邀请码加入房间

[英]Joining a room using invite code in flutter

I'm trying to create a simple game application using flutter. It has a page where users can enter an invite code and press a button to join the room.我正在尝试使用 flutter 创建一个简单的游戏应用程序。它有一个页面,用户可以在其中输入邀请码并按下按钮加入房间。 I have multiple rooms with different names on firebase and each one has a unique inviteCode.我在 firebase 上有多个不同名称的房间,每个房间都有一个唯一的邀请码。 How can I write a function to check through all the rooms if the enteredCode matches any of the rooms' invite codes?如果 enteredCode 与任何房间的邀请代码匹配,我如何编写 function 来检查所有房间?

This is what I've tried so far:到目前为止,这是我尝试过的:

Future<bool> _checkInviteCode(String enteredCode) async {
    // reference to the Firestore
    final firestore = FirebaseFirestore.instance;
    //get the invite code collection
    final querySnapshot = await firestore.collectionGroup('inviteCode').get();

    // check if the entered code matches any of the invite codes in the Firestore collection
    if (querySnapshot.docs
        .any((doc) => doc.data()['inviteCode'] == enteredCode)) {
      print("Code matched!");
      return true;
    } else {
      print("Invalid code");
    }
    return false;
  }

There is no way in the Firestore API to query across multiple collections with different names.在 Firestore API 中无法跨多个具有不同名称的 collections 进行查询。 The only options are to query a single collection, or to query all collections with one name using a collection group query .唯一的选择是查询单个集合,或者使用集合组查询查询同一个名称的所有 collections。


So you will either have to query each collection (name) individually and merge the results in your application code, or you'll have to adapt your data model to fit the use-case (which is quite common in NoSQL databases).因此,您要么必须单独查询每个集合(名称)并将结果合并到您的应用程序代码中,要么您必须调整数据 model 以适应用例(这在 NoSQL 数据库中很常见)。

For example, consider creating a collection inviteCodes where you (also) store the codes using the invite code as the document ID and then include a field that points to the document path for the user (or whatever document type you have) that holds that invite code.例如,考虑创建一个集合inviteCodes ,您(也)在其中使用邀请代码作为文档 ID 存储代码,然后包括一个指向持有该邀请的用户(或您拥有的任何文档类型)的文档路径的字段代码。 Having such a table would not only make this use-case simpler, but it will also immediately ensure that only one user can hold each invite code.拥有这样一张表不仅会使这个用例更简单,而且还会立即确保只有一个用户可以持有每个邀请码。

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

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