【问题标题】:Flutter Firestore NoSuchMethodError: The method 'get' was called on nullFlutter Firestore NoSuchMethodError:在 null 上调用了方法“get”
【发布时间】:2019-03-10 14:14:26
【问题描述】:

我有一个 Firestore 数据库。我想知道的是如何检查该数据库中是否存在某个文档引用。以前可以用的代码,现在坏了。

获取参考

travelTotalReference =
        Firestore.instance.document('$_id/$_presentDate BalanceTotal');

检查数据是否存在

 travelTotalReference.get().then((dataSnapshot) {
        if (dataSnapshot.exists) {
          setState(() {
            travel = dataSnapshot.data['total'];
          });
        }else{
          setState(() {
            travel = 0;
          });
        }
      });

顺便说一句,我知道这个参考不存在。我只是想确保如果可以,我可以更新 travel 变量。

我得到的错误

E/flutter (17184): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: 
Unhandled exception:
E/flutter (17184): NoSuchMethodError: The method 'get' was called on null.
E/flutter (17184): Receiver: null
E/flutter (17184): Tried calling: get()
E/flutter (17184): #0      Object.noSuchMethod 
(dart:core/runtime/libobject_patch.dart:48:5)

【问题讨论】:

  • 显然travelTotalReferencenull,而您并不期望它是。您在问题中的代码缺少可以诊断问题的基本部分。

标签: firebase dart flutter google-cloud-firestore


【解决方案1】:

如果你调用一个不存在的对象,你怎么能指望你的travelTotalReference 不为空。

从那以后调用get()返回一个错误,看起来很公平......

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 2021-09-29
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2020-05-23
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多