Cloud Firestore error The operator '[]' isn't defined for the type 'Object'. after Upgrade to 2.0.0?

Issue

This is NOT the old doc.data[‘field’] vs. doc.data()[‘field’] migration issue…it’s new (to me):

The following code is a common example in my project running fine for the last 8 mos in Flutter with Firebase. I’m migrating to null safety, and in upgrading my dependencies to latest versions the following code is returning the following IDE error: "The operator ‘[]’ isn’t defined for the type ‘Object’."

  final CollectionReference openUserList = FirebaseFirestore.instance.collection('openUsers');

final newUser = await openUserList.doc(ownerEmail.toLowerCase()).get();
if (newUser.exists) {
  return UserEssential(
    uid: newUser.data()['uid'],
    email: newUser.data()['email'].toString().toLowerCase(),
    displayName: newUser.data()['displayName'],
    defaultFacility: newUser.data()['facility'] ?? '',
    phone: newUser.data()['phone'] ?? '',
  );
} else {
  return null;
} 

When I adjust to the following, specifying the place in newUser as if it is a list of newUsers, it runs:

    uid: newUser[0].data()['uid'],
    email: newUser[0].data()['email'].toString().toLowerCase(),
    displayName: newUser[0].data()['displayName'],
    defaultFacility: newUser[0].data()['facility'] ?? '',
    phone: newUser[0].data()['phone'] ?? '',

No errors after making that change. I haven’t seen or found any documentation on this on Flutter Fire. Did DocumentSnapshot become a list?

Is this related to null safety, an undocumented firebase change, and IDE bug or something else? My code example pulls a DocumentSnapshot, so there can’t be more than one newUser, why require the [0] and since this coincides with my null safety migration it’s happening along with a lot of other things. I’m worried that this is actually wrong, and it’s something else and I’m wasting time.

BTW: explicitly declaring newUser as a DocumentSnapshot doesn’t change anything.

Solution

Ok, after reading the FlutterFire Docs and 2.0.0 migration a little more closely here:

The solution is just to define the CollectionReference more explicitly like so:

  final CollectionReference<Map<String, dynamic>> openUserList = FirebaseFirestore.instance.collection('openUsers');

Answered By – jbryanh

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.