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

Solution 1:

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,
      ),
    );
  }
),

Solution 2:

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.