flutter/dart 如何检查firestore中是否存在文档? [英] flutter / dart How can check if a document exists in firestore?

查看:24
本文介绍了flutter/dart 如何检查firestore中是否存在文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 bool 查询 cloudfirestore 上是否存在文档.不幸的是,我的代码不起作用

I try to query using a bool if a document exists on the cloudfirestore or not. Unfortunately, my code does not work

我尝试了以下方法,但布尔值没有改变.

I tried the following, but the bool does not change.

getok() {
  bool ok;
  Firestore.instance.document('collection/$name').get().then((onexist){
      onexist.exists ? ok = true : ok = false;
    }
  ); 
  if (ok = true) {
    print('exist');
  } else {
    print('Error');
  }
}

推荐答案

你可以尝试这样做,虽然我使用的是 IDS

You could try doing it this way though im usi IDS

//Declare as global variable

bool exist;

static Future<bool> checkExist(String docID) async {       
    try {
        await Firestore.instance.document("users/$docID").get().then((doc) {
            exist = doc.exists;
        });
        return exist;
    } catch (e) {
        // If any error
        return false;
    }
}

这篇关于flutter/dart 如何检查firestore中是否存在文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆