【问题标题】:How to fix "Unhandled Exception: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist"?如何修复“未处理的异常:错误状态:无法在 DocumentSnapshotPlatform 上获取不存在的字段”?
【发布时间】:2020-12-03 02:30:40
【问题描述】:

我有一个函数可以在导航到另一个页面之前从 firebase 获取一个值。不幸的是,当调用该函数时,我收到此错误“未处理的异常:错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段”。但是,如果我热重新加载我的应用程序,则会调用该函数并且该值在那里。你们知道我该如何解决这个问题吗?

函数如下:

  Future<void> getDeliveryPrice(String profileId) async {
    final DocumentSnapshot doc = await storeRef.doc(profileId).get();

    final price = doc['delPrice'] as num;

    deliveryPrice = price;
    print(profileId);

    notifyListeners();
  }

这里是我调用函数的地方:

  PriceCard(
     buttonText: 'Continuar para a Entrega',
     onPressed: cartManager.isCartValid
         ? () {
            cartManager.getDeliveryPrice(profileId);
            print(profileId);

             Navigator.push(
                  context, MaterialPageRoute(
             builder: (context) => AddressScreen(profileId),
             ));
            } : null,
          )

【问题讨论】:

  • doc.data()['delPrice'] as num;,我不知道这里是否有区别。但我认为你应该得到它的 .data().. 你的 cloud_firestore 的 pakacge 版本是什么?
  • doc 是DocumentSnapshot 类型,它有一个方法data(),它返回一个包含文档字段和值的地图
  • 我已经尝试过了,但它不起作用。该功能在没有data() 的情况下有效,但前提是我热重载应用程序,但当我调用它时它不会。

标签: firebase flutter


【解决方案1】:

查看this GitHub issue。它表示该错误与DocumentSnapshot 的最近更改有关。数据以前在doc,现在在doc.data()

【讨论】:

  • 感谢您的回答 Mickael,我已经尝试过了,但它不起作用。该函数在没有data() 的情况下工作,但只有当我热重载应用程序时,但当我调用它时它不会。
  • 这可能是因为您没有收听更新。使用Consumer/Selector 小部件或context.watch&lt;T&gt;() 函数。
【解决方案2】:

我找到了问题,profileId 丢失了。

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 2021-05-16
    • 2021-03-20
    • 2021-03-05
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2021-05-19
    • 2021-06-17
    相关资源
    最近更新 更多