After updating Flutter Firebase receive Bad State: field does not exist - QueryDocumentSnapshot

Viewed 48

Prior to the update, document['field'] would result in null if it did not exist. Now it throws a Bad State error. The null response is needed because the field does not exist in some historical data and/or optional data in the Firebase collection. Confirmed this in the following report as well:

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

Is there a way to capture the error and ignore it or set it to null or empty string?

 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();
}
 
1 Answers

With null safety, String is not nullable. However, String? is.

Change the line:

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

To:

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

Your Pool will also have to have the property propertyManagerID as a String? in the class definition.

Related