Reputation: 471
I updated my Firestore package and I am getting this error ("The operator '[]' isn't defined for the type Map <String, dynamic> Function()"). I saw that there in another post about the same error here, but in my case, I am unsure how to make the changes so I am starting a new thread with my code. Can anyone show me how to make the changes, please? The error is showing up in the code below at List categories = document.data['listCategories'] ?? [];
TextFormField(
controller: _catController,
obscureText: false,
autocorrect: false,
onChanged: (value) {},
decoration: InputDecoration(
labelText: 'Add a new category here',
labelStyle: TextStyle(
color: Theme.of(context).primaryColor),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 2,
color: Theme.of(context).primaryColor),
),
suffixIcon: IconButton(
icon: Icon(
// Based on passwordVisible state choose the icon
Icons.add,
color: Theme.of(context).primaryColor,
),
onPressed: () async {
final String name = _catController.text;
DocumentReference docRef =
userCollection.doc(_uid);
DocumentSnapshot document = await docRef.get();
List categories =
document.data['listCategories'] ?? []; // <-- ERROR
if (categories.contains(name) == true) {
setState(() {
_errorMessage =
'$name has already been added';
});
} else {
userCollection.doc(_uid).update({
'listCategories':
FieldValue.arrayUnion([name])
});
_catController.text = '';
setState(() {
_errorMessage = '';
});
}
},
),
),
autovalidateMode: AutovalidateMode.always,
cursorColor: Theme.of(context).primaryColor,
maxLines: 1,
),
Upvotes: 0
Views: 1500
Reputation: 1039
This error The operator '[]' isn't defined for the type Map <String, dynamic> Function()
tells you that the object you are trying to do the []
operator on is a function. In your case, .data
is actually not a member variable but a function. It is solved by just adding ()
next to the data
keyword, so your error line (fixed) would look like that:
document.data()['listCategories'] ?? [];
That might've happened because you updated the firebase core package. It used to be just .data[]
but now it's .data()[]
.
Upvotes: 2