How can I resolve " Error: The operator '[]' isn't defined for the class 'Object?'"

0

Issue

appBar: AppBar(
    title: StreamBuilder<DocumentSnapshot>(
        stream:  _firestore.collection('users').doc(userMap['uid']).snapshots(),
        builder: (context, snapshot){
          if (snapshot.data != null){
            return Container(
              child: Column(
                children: [
                  Text(userMap['name']),
                  Text(
                    snapshot.data!.data()['status'],
                    style: TextStyle(fontSize: 14),
                  )
                ],
              ),
            );
          }else{
            return Container();
          }
        }

I try to check the status of the user and show in the appbar if the user is online or offline, I already create a variable for that and It’s work proprely but when I want to show the status in the appbar I have this error.

snapshot.data!.data()['status'],

the error is on this line
if somoene can help that will be cool.

Solution

Try this:
(snapshot.data!.data() as Map<String, dynamic>)['status'],

Dart doesn’t know which type of object it is getting.

See reference:
The operator '[]' isn't defined for the class 'Object'. Dart

Answered By – J.K.

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More