The operator ‘[]’ isn’t defined for the type ‘Object? Function()’. Try defining the operator ‘[]’

Solution for The operator ‘[]’ isn’t defined for the type ‘Object? Function()’. Try defining the operator ‘[]’
is Given Below:

I recently started a flutter project and migrated some old firebase code to the new one. I then updated firebase and fixed most of the errors until this one stumped me.

final firestore = FirebaseFirestore.instance; //
FirebaseAuth auth = FirebaseAuth.instance;

Future<bool?> addPlastic(String amount) async {
  try {
    String uid = auth.currentUser!.uid;
    var value = double.parse(amount);

    DocumentReference documentReference =
        FirebaseFirestore.instance.collection('Users').doc(uid);

    FirebaseFirestore.instance.runTransaction((transaction) async {
      DocumentSnapshot snapshot = await transaction.get(documentReference);
      if (!snapshot.exists) {
        documentReference.set({'plasticsCollected': value});
        return true;
      }
      double newAmount = snapshot.data()!['plasticsCollected'] + value;
      transaction.update(documentReference, {'plasticsCollected': newAmount});

      return true;
    });
  } catch (e) {
    return false;
  }
}

Here is where I am getting the error “The operator ‘[]’ isn’t defined for the type ‘Object? Function()’. Try defining the operator ‘[]’.”

double newAmount = snapshot.data()!['plasticsCollected'] + value;

What am I doing wrong?

After messing around a bit I ended up changing:

double newAmount = snapshot.data()!['plasticsCollected'] + value;

To

double newAmount = snapshot.get('plasticsCollected') + value;

Now it works for me.