Home > Blockchain >  The operator '[]' isn't defined for the type 'Object'. Try defining the ope
The operator '[]' isn't defined for the type 'Object'. Try defining the ope

Time:01-07

I am getting following error: The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]' The part where error is coming is highlighted here

The code for stream is this

StreamBuilder(
                stream: FirebaseFirestore.instance
                    .collection('Users')
                    .doc(FirebaseAuth.instance.currentUser?.uid)
                    .collection('Coins')
                    .snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (!snapshot.hasData) {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }
                  return ListView(

                    children: snapshot.data!.docs.map((document) {
                      return Container(
                        
                        child: Row(
                          children: [
                            
                            Text("Coin type: ${document.id}"),
                            
                            Text("Amount Owned: ${document.data()!['Amount']}"),
                          ],
                        ),
                      );
                    }).toList(),
                  );
                }),

CodePudding user response:

You can use as Map?

Text(
    "Amount Owned: ${(document.data() as Map?)?['Amount']}"),

More about dart and reading data from firestore.

  • Related