"The operator '[]' isn't defined" error when using .data[] in flutter firestore
Change this:
name: doc.data['name'] ?? ''
Into this:
name: doc.data()['name'] ?? ''
data()
is a method now therefore you have to add ()
, from the source code:
Map<String, dynamic> data() {
return _CodecUtility.replaceDelegatesWithValueInMap(
_delegate.data(), _firestore);
}
https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/document_snapshot.dart#L38
For me, worked like this:
return snapshot.docs.map((doc) {
return Todo(
// before
title: doc.data()['title'],
// after
title: (doc.data() as dynamic)['title'],
);
}).toList();
In pubspec.yaml:
environment:
sdk: ">=2.12.0 <3.0.0"
...
cloud_firestore: ^2.3.0
firebase_core: ^1.3.0
Firestore's data
used to be a property of QueryDocumentSnapshot
, but now it is a function, data()
.
And, as the error message suggests, what you are dealing with is indeed a Map<String, dynamic>
Function()
, i.e. a function that returns a map.
So, simply add empty parentheses to call the function data
:
doc.data()['name']