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

Viewed 11593

My code is given below the error I am facing issue in accessing the username from firebase in this line

snapshot.data['username']

It gives the error mentioned above

The only way I know to access the map data is this

FutureBuilder<Object>(
  future: FirebaseFirestore.instance
   .collection('users')
   .doc(userId)
   .get(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return Text("Loading...");
    }
    return Text(
      snapshot.data['username'],
      style: TextStyle(
        fontWeight: FontWeight.bold,
      ),
    );
  }
),
10 Answers

In the new flutter update, we don't need to add .data()

I got this error and after removing .data() it was solved.

FutureBuilder<Object>(
  future: FirebaseFirestore.instance
   .collection('users')
   .doc(userId)
   .get(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return Text("Loading...");
    }
    return Text(
      snapshot['username'],
      style: TextStyle(
        fontWeight: FontWeight.bold,
      ),
    );
  }
),

In the newer versions of Flutter you have to specify the type for FutureBuilder as DocumentSnapshot. Edit your code like below:

           FutureBuilder<DocumentSnapshot>(
              future: FirebaseFirestore.instance
                  .collection('users')
                  .doc(userId)
                  .get(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) 
                {
                  return const Text('Loading...');
                }
                return Text(
                  snapshot.data!['username'],
                  style: const TextStyle(fontWeight: FontWeight.bold),
                );
              }),

This should work for you.

This solution worked for me!

Try switching

snapshot.data['username']

with

(snapshot.data as DocumentSnapshot)['username']

Use this format for the updated version of Flutter.

FutureBuilder<DocumentSnapshot>(
                  future: FirebaseFirestore.instance
                      .collection('users')
                      .doc(userId)
                      .get(),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Text("Loading..");
                    }
                    return Text(
                      snapshot.requireData['username'],
                      style: TextStyle(fontWeight: FontWeight.bold),
                    );
                  }),

This solution is for the lecture number 336 at 4:30mins of the Udemy Course of Flutter A Complete Guide to the Flutter SDK & Flutter Framework for building native iOS and Android apps . Created by, Academind by Maximilian Schwarzmuller

snapshot.data is the data returned by FutureBuilder. So technically snapshot.data is a type of DocumentSnapshot. To access this document's data, you should use snapshot.data.data() or this code below:

FutureBuilder<Object>(
  future: FirebaseFirestore.instance
   .collection('users')
   .doc(userId)
   .get(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      DocumentSnapshot doc = snapshot.data;
      return Text(
        doc.data()['username'],
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      );
    }
    return Text("Loading...");
  }
),

To the updated version, you have to specify the type for FutureBuilder as DocumentSnapshot. Also the data is changed to requiredData. Edit your code like below:

       FutureBuilder<DocumentSnapshot>(
          future: FirebaseFirestore.instance
              .collection('users')
              .doc(userId)
              .get(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) 
            {
              return const Text('Loading...');
            }
            return Text(
              snapshot.requiredData['username'],
              style: const TextStyle(fontWeight: FontWeight.bold),
            );
          }),

And now your code will work perfectly.

Change this:

  snapshot.data['username'],

into this:

  snapshot.data.data()['username'];

data() is a method

this is very easy to do and it worked for me.

  FutureBuilder<Object>(
  future: FirebaseFirestore.instance
   .collection('users')
   .doc(userId)
   .get(),
  builder: (context, snapshot) {
   var data=snapshot.data as Map;
    if (snapshot.connectionState == ConnectionState.waiting) {
      return Text("Loading...");
    }
    return Text(
      data['username'],
      style: TextStyle(
        fontWeight: FontWeight.bold,
      ),
    );
  }
),

do that : add ( AsyncSnapshot) as that

builder: (context, AsyncSnapshot snapshot) {
                if (ConnectionState.waiting == snapshot.connectionState) {
                  return const Text('...chargement');
                }

                return Text(snapshot.data['name']);
              }),

(snapshot.data() as Map<String, dynamic>) ['your field']; //This actually worked for me try it...

Related