Flutter firestore QuerySnapshot has no instance of getter ‘documents’ in Android

I am trying to retrieve the values of the variables from the firestore cloud. Through implementing the code, I've seemed to come across an error, and I've spent hours getting frustrated over this😭, trying to find solutions here and elsewhere. The error I've faced alongside the code is as per below.

The following NoSuchMethodError was thrown building StreamBuilder(dirty, state:_StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot>#4939b): Class 'QuerySnapshot' has no instance getter 'documents'. Receiver: Instance of 'QuerySnapshot' Tried calling: documents

Error Displayed on the Virtual Device

Code that I am using:

 Widget buildStreamBuilder() {
     return Container(
        child: !_success
            ? Center(
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
          ),
        )
            : StreamBuilder(
          stream: FirebaseFirestore.instance.collection('sensors').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
                ),
              );
            } else {
              snapshot.data.documents.forEach((doc) {
                if (doc.documentID == 'cSBKpiEe1XKmQC8BDzMk') {
                  print('current value = ${doc['BatStat']}'); //var1
                  globalCurrentSensorValue = doc['BatStat'].toDouble();
                }
              });
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Container(),
                    ),
                    Expanded(
                      flex: 1,
                      child: Container(),
                    )
                  ],
                ),
              );
            }
          },
        ));
  }


Read more here: https://stackoverflow.com/questions/65705402/flutter-firestore-querysnapshot-has-no-instance-of-getter-documents-in-android

Content Attribution

This content was originally published by ZenithAoa at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: