简体   繁体   English

Flutter Firebase - 无法从 Firestore 获取数据

[英]Flutter Firebase - Not able to fetch data from Firestore

I am trying to get the data from "savedGoal" collection and somehow it is throwing error (NoSuchMethodError).我正在尝试从“savedGoal”集合中获取数据,但它以某种方式抛出错误(NoSuchMethodError)。 However same script/syntax of code is working absolutely fine for another collection on same firebase project.然而,相同的代码脚本/语法对于同一个 firebase 项目的另一个集合绝对可以正常工作。 Please let me know what could be wrong here.请让我知道这里可能出了什么问题。 Here is the script that I am running in ininState()-这是我在 ininState() 中运行的脚本-

FirebaseFirestore.instance
        .collection("savedGoals")
        .where('uid', isEqualTo: FirebaseAuth.instance.currentUser.uid)
        .get()
        .then((value) {
      value.docs.forEach((result) async {
        if (result.data().isNotEmpty) {
          setState(() {
            goalAmount = result.data()['goalAmountFixed'];
          });
        }

Here is the screenshot of error that I am receiving while using "savedGoal" collection - enter image description here这是我在使用“savedGoal”集合时收到的错误截图 -在此处输入图像描述

Here is the firebase document screenshot- enter image description here这是 firebase 文档截图 -在此处输入图像描述

Check in main.dart that if you have intialized firebase correctly.检查 main.dart 是否正确初始化了 firebase。 In case if you have not initialzed firebase in your project then try initializing it by adding this line in main:如果您尚未在项目中初始化 firebase,请尝试通过在 main 中添加以下行来初始化它:

main() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,

); ); } }

You don't need to call.data() to get the value of the key.您不需要 call.data() 来获取键的值。 just get the value of your key directly from QuerySnapshot as I did below.就像我在下面所做的那样,直接从 QuerySnapshot 获取密钥的值。

FirebaseFirestore.instance
    .collection("savedGoals")
    .where('uid', isEqualTo: FirebaseAuth.instance.currentUser!.uid)
    .get()
    .then((value) {
  for (var result in value.docs) {
    if (result.data().isNotEmpty) {
      setState(() {
        goalAmount = result['goalAmountFixed'];
      });
    }
  }
});

Mark this answer as correct if it helped and solved your issue or if you are still having issues then please comment on this answer I will help.如果它帮助并解决了您的问题,或者如果您仍然遇到问题,请将此答案标记为正确,然后请评论此答案,我会提供帮助。

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

相关问题 Not able to fetch data from Firebase Firestore SHOWS ERROR in NEXT.JS using Firebase SDK Firebase v9 - Not able to fetch data from Firebase Firestore SHOWS ERROR in NEXT.JS using Firebase SDK Firebase v9 使用 flutter 从 firebase Firestore 获取数据 - fetching data from firebase firestore using flutter 如何从 firebase firestore 获取数据并将数据存储在 useState 钩子中 - How to fetch data from firebase firestore and store the data in useState hook React Native 如何从 Firebase Firestore 获取数组数据? - React Native how to fetch Array data from Firebase Firestore? 从 Firestore 获取字段作为 flutter 中的字符串 - Fetch field from firestore as String in flutter Flutter - 无法将数据保存到 Firebase Firestore - Flutter - Unable to save data to Firebase Firestore 从 firebase 获取数据 - Data fetch from firebase 在 flutter 中,我们可以从插入数据的地方的 firestore 获取数据吗? - In flutter can we fetch data from firestore where the place data insert? 从 Firestore Flutter 保存数据 - Save data from Firestore Flutter 如何使用 where 子句从 firebase 正确获取数据,并使用 flutter 从 firebase 中正确获取数据 - How to correctly fetch data from firebase using where clauses and custom filters from firebase using flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM