【发布时间】: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()的情况下有效,但前提是我热重载应用程序,但当我调用它时它不会。