firestore the getter 'length' isn't defined for the type 'DocumentSnapshot<Object?>'


I try to count all docs from one user in firestore.
My code:

  Widget booksWidget(String userData) {                            
        stream: FirebaseFirestore.instance
            .orderBy("timestamp", descending: true)
        builder: (BuildContext context,AsyncSnapshot snapshot)  {
          if (snapshot.hasData) {
            var userDocument = as DocumentSnapshot?;
            String books = userDocument?.length.toString();
            return Text(books);
            return Text("none");

the error:

The getter 'length' isn't defined for the type 'DocumentSnapshot<Object?>'.

thanks for help, streambuilder after migration to null-safety is quite different 🙁


You’re requesting the snapshots of a query, so the that you get is going to be of type QuerySnapshot (and not a DocumentSnapshot? as you assume now).

if (snapshot.hasData) {
  var querySnapshot =! as QuerySnapshot;
  String books =;

In cases like this I find it easiest to work from the reference documentation, such as the one for Query.snapshots() here.

Answered By – Frank van Puffelen

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *