Firestore查询简单的获取查询不起作用。

huangapple go评论54阅读模式
英文:

firestore query simple get query not working

问题

在这个代码中,Catch块没有被触发,而且打印输出显示为null。但是我在Firestore中有数据。

请帮忙。

请注意,这是您的原始文本的中文翻译。如果您需要进一步的帮助,请提出具体的问题或附加信息。

英文:

I must be doing something stupid as I have written many complex queries but somehow, this query is not working.

OnboardingUser Newuser = OnboardingUser();
    try {
      var user = await FirebaseFirestore.instance
          .collection("users")
          .doc("EOLjcTCCvMfsi9mnbNynAGwCo723")
          .get();
      print("user: " + user.data().toString());
      Newuser = OnboardingUser.fromFirestore(user, null);
    } catch (e) {
      Logger().e(className + ": " + "getUserByUid: " + e.toString());
    }

Here Catch block is not triggered and print is showing null in output i.e.

user: null

But I have the data in the firestore
Firestore查询简单的获取查询不起作用。

Kindly help.

答案1

得分: 3

我认为问题可能是查询未以正确的格式返回用户文档。

以下是创建用于从用户集合中检索项目的转换器设置。您需要在您的OnboardingUser类中设置fromFirestore和toFirestore函数。

...
get onboardingUserRef {
    return _db.collection("users").doc(_id).withConverter(
        fromFirestore: OnboardingUser.fromFirestore,
        toFirestore: (OnboardingUser user, _) => user.toFirestore(),
    );
}

无论您的功能是什么,都需要这样做:
async {
    try {
        userRef = await onboardingUserRef.get();
        OnboardingUser user = userRef.data();
    } catch (e) {
        // 捕获错误
    }

}
...
英文:

I believe the issue might the query might not be returning to user document in the correct format.

Below shows the set up of creating converter for your collection to retrieve an item from your users collection. you will need to have both the fromFirestore and toFirestore functions set up in your OnboardingUser class.

...
get onboardingUserRef {
    return _db.collection("users").doc(_id).withConverter(
        fromFirestore: OnboardingUser.fromFirestore,
        toFirestore: (OnboardingUser user, _) => user.toFirestore(),
    );
}

whateverYourFunctionIs() async {
    try {
        userRef = await onboardingUserRef.get();
        OnboardingUser user = userRef.data();
    } catch (e) {
        // catch error
    }

}
...

huangapple
  • 本文由 发表于 2023年6月22日 16:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定