【发布时间】:2021-12-04 08:15:16
【问题描述】:
我在 Flutter Firestore 2.5.3 中遇到了一个奇怪的错误,如果我向 Firebase 控制台中的文档添加新字段 (customer_type),应用程序无法检索新添加的字段并引发异常:
CastError (type 'Null' is not a subtype of type 'String' in type cast)
但它不为空。我使用 Firebase 控制台中的值添加了新字段。它在文档中。原来 Firebase 正在获取旧文档数据没有新添加的字段。
我正在使用来自 Riverpod 的 StreamProvider。代码如下:
static Stream<Customer?> _watchCustomer(final ProviderRefBase ref) async* {
final auth = await ref.watch(authProvider.last);
if (auth == null) {
yield null;
} else {
final customerRef = _db.collection('customers').toCustomerRef(auth.uid);
await for (var customer in customerRef.snapshots().map((doc) => doc.data())) yield customer;
}
}
可能出了什么问题?
编辑:
所有名称、集合和字段都是正确的,它们的类型也是正确的。如果我重新安装应用程序或完全清除应用程序存储,相同的确切代码将正常工作。但是,如果我只是清除缓存,它就不起作用。它在调试时阻碍了我的工作流程,我想知道为什么会这样。如需更多参考,这是我的“客户”模型:
Customer.fromMap(final String documentID, final Map<String, dynamic> map)
: id = documentID,
name = map['customer_name'] as String,
mobile = map['customer_mobile'] as String,
type = map['customer_type'] as String;
它设置为不可为空,因为我正在建立一个严格的架构。但这不应该是一个问题,因为它在数据库中不是空的。我已将customer_type 添加到集合中的所有文档中。而且它们都没有将值设置为 null。
这只能意味着我从 Firestore 获得的地图找不到必要的密钥。这意味着我仍在从 Firestore 获取旧数据(没有新字段)。是什么赋予了?至少当我清除缓存时,它应该会给我更新的数据。
【问题讨论】:
-
您的集合“客户”在您的 FB 控制台中的名称/拼写是否完全相同?
-
另外可能是数据快照本身有问题,能否提供完整的错误信息?
-
@JahnE。这就是完整的错误。它基本上中途停止应用程序并向我显示调试消息。是的,“客户”是完全相同的集合名称 - 拼写正确。它以前工作得很好。当我添加新字段时它就停止工作了。
标签: firebase flutter google-cloud-firestore stream riverpod