how to fix the "The operator '[]' isn't defined for the type 'Object'" error while getting data from snapshot?


i have this function

UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
    return UserData(
      uid: uid,

but im getting error on the paranthesis

The operator '[]' isn't defined for the type 'Object'.

updateee for the question

also i have the function and instance of collection

final CollectionReference users = FirebaseFirestore.instance.collection('users');

 // get user doc stream
  Stream<UserData> get userData {
    return users.doc(uid).snapshots()

when i read data:

Widget profile(context, user){
    return StreamBuilder<UserData>(
      stream: DatabaseService(uid: user.uid).userData,
      builder: (context, snapshot) {
          UserData userData =;
         } else print('no data');

what can i do?

im using versions:

Flutter 2.0.1 • channel stable •
Framework • revision c5a4b4029c (1 year, 1 month ago) • 2021-03-04 09:47:48 -0800
Engine • revision 40441def69
Tools • Dart 2.12.0


You are approaching to the data in two different ways. One of them (which is the first one) is correct but the other ways are wrong. Once you have the document snapshot, get the data out of it and use it as an assigning mechanism.

You can try the code below.

For more information –>

UserData _userDataFromSnapshot(DocumentSnapshot document) {
    final data =! as Map<String, dynamic>;
    return UserData(
      uid: uid,
      userName: data['userName'],
      userEmail: data['userEmail'],
      phoneNumber: data['phoneNb']

Answered By – salihgueler

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *