【问题标题】:After updating Flutter Firebase receive Bad State: field does not exist - QueryDocumentSnapshot更新 Flutter Firebase 后收到 Bad State: field does not exist - QueryDocumentSnapshot
【发布时间】:2021-03-17 21:28:58
【问题描述】:

在更新之前,如果 document['field'] 不存在,它将导致 null。现在它会抛出一个 Bad State 错误。需要 null 响应,因为该字段在 Firebase 集合中的某些历史数据和/或可选数据中不存在。在以下报告中也证实了这一点:

https://github.com/FirebaseExtended/flutterfire/issues/3826

有没有办法捕获错误并忽略它或将其设置为 null 或空字符串?

 static Pool dataFromDocument(QueryDocumentSnapshot document) {
  return Pool()
    ..authUID = document.get('authUID')
    ..documentID = document.id
    ..propertyManagerID = document['propertyManagerID'] as String
 }

    static Stream<QuerySnapshot> getData(String authUID) {
CollectionReference poolRef = FirebaseFirestore.instance.collection(dataDB);
Query query = poolRef.where('authUID', isEqualTo: authUID.trim());
return query.snapshots();
}
 

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    使用 null 安全性,String 不可为 null。但是,String? 是。

    换行:

    ..propertyManagerID = document['propertyManagerID'] as String
    

    收件人:

    ..propertyManagerID = document['propertyManagerID'] as String?
    

    您的Pool 还必须在类定义中将属性propertyManagerID 作为String?

    【讨论】:

    • 不幸的是,它在到达字符串之前就出错了? - 完整的错误消息是:I/flutter (28251): Bad state: DocumentSnapshotPlatform 中不存在字段。如果字段丢失,我在更新错误之前将其设置为传递字符串,但是当我看到它没关系时将其删除:document['propertyManagerID'] ?? '' 作为字符串
    猜你喜欢
    • 2023-01-29
    • 2021-10-01
    • 2020-05-27
    • 2023-01-12
    • 2023-02-23
    • 2020-10-12
    • 2015-09-14
    • 2015-10-24
    • 2018-11-10
    相关资源
    最近更新 更多