【问题标题】:How to check if a document with a field having some value exists and return true/false?如何检查具有某个值的字段的文档是否存在并返回真/假?
【发布时间】:2020-10-09 00:39:27
【问题描述】:

我有一个 Firebase Firestore 数据库。我想检查我的数据库中是否存在包含包含一些数据的字段的文档。如果存在则返回 true,否则返回 false。

我在 onPressed 函数中使用它,这就是为什么我需要一个 true 或 false 的值,因为如果它是 true,其他任务可以继续,但如果不是,我想显示一条错误消息。这是某些特定用户的邀请码,我想检查某些用户是否有该邀请码,这意味着邀请码是正确的。

我试过了

var users = FirebaseFirestore.instance
    .collection('users')
    .where('invitation_code', isEqualTo: inputCode)
    .get();

但我不能使用“users.length”来查看长度是否大于0。

我正在使用 Flutter。

你能帮帮我吗?

谢谢!

【问题讨论】:

  • get() 异步返回 Future<QuerySnapshot>。您需要像处理 dart 中的任何其他 Future 一样处理该 Future。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

你可以检查它的文档是否为空

FirebaseFirestore.instance
        .collection('users')
        .where('invitation_code', isEqualTo: inputCode)
        .get()
        .then((value) {
      if (value.docs.isNotEmpty) {
        // return true;
      } else {
        // return false;
      }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2022-08-18
    • 1970-01-01
    相关资源
    最近更新 更多