How to get Firebase snapshots length?

1,960

Try to explicitly declare type in your StreamBuilder

StreamBuilder<QuerySnapshot>(
        stream: _firestore.collection('...').snapshots(),

https://dart.dev/guides/language/sound-problems#runtime-errors

Share:
1,960
dfdfdsfsd
Author by

dfdfdsfsd

Updated on December 15, 2022

Comments

  • dfdfdsfsd
    dfdfdsfsd over 1 year

    I've created an app with Firebase but I've got a problem, which is that I

    can't count the snapshot length in listview.builder.

    StreamBuilder(
            stream:
                _firestore.collection('/posts/${widget.uid}/comments').snapshots(),
            builder: (context, snapshot) {
              final data = snapshot.data.documents;
    
              if (!snapshot.hasData) {
                return SpinKitChasingDots(
                  color: Colors.lightGreen,
                );
              }
              return ListView.builder(
                itemCount: snapshot.data.documents.length,//Here is the problem
                itemBuilder: (context, index) {
                  return PostComment(
                    commentText: data[index]['commentText'],
                    userImage: data[index]['userImage'],
                    userName: data[index]['userName'],
                  );
                },
              );
            },
          ),
    
    

    When I run it gives me an error that it doesn't have actually a getter called length.

  • dfdfdsfsd
    dfdfdsfsd over 4 years
    everything is going okay except displaying it as I've mentioned above
  • Dennis Alund
    Dennis Alund over 4 years
    Can you confirm that you tried the answer and it didn't help?
  • dfdfdsfsd
    dfdfdsfsd over 4 years
    I've not noticed your data type declaring and it worked, thanks
  • Community
    Community over 1 year
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.