How to retrieve data from firebase firestore into flutter using StreamBuilder and also used ListView.builder to show the values?

1,707

change this:

builder: (BuildContext context, AsyncSnapshot<dynamic> streamSnapshot) =>

into this:

builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> streamSnapshot) =>

Then in the itemCount you can do:

itemCount: streamSnapshot.data.docs.length
Share:
1,707
Mijanur Rahman
Author by

Mijanur Rahman

A Software Engineer specializing in Web and Mobile app Development. I'm a developer with both front-end and back-end focus. I've been building software professionally for over 2 years and I've loved every minute of it! The technologies that I use: ReactJS, NodeJS, Flutter, MongoDB, MySQL , Java with OOP, Android Material Design, and more.

Updated on December 28, 2022

Comments

  • Mijanur Rahman
    Mijanur Rahman over 1 year

    If i downgrade the version it works but i am facing problems when i use the below version.

    cloud_firestore: ^1.0.0

    firebase_core: ^1,0.0

    And flutter sdk version: 2.0.1

    I am trying to imaplement the code. streamSnapshot.data.length does not work

    body: StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection('chats/UMrLSClddfvaw9wV0hs6/messages')
                .snapshots(),
            builder: (BuildContext context, AsyncSnapshot<dynamic> streamSnapshot) =>
                streamSnapshot.connectionState == ConnectionState.none
                    ? Center(
                        child: CircularProgressIndicator(),
                      )
                    : ListView.builder(
                        itemCount: streamSnapshot.data,
                        itemBuilder: (context, index) => Container(
                          padding: EdgeInsets.all(8),
                          child: Text('This works'),
                        ),
                      ),
          ),
    
    • Peter Haddad
      Peter Haddad about 3 years
      what problem are you facing?
    • Mijanur Rahman
      Mijanur Rahman about 3 years
      itemCount needs length. But how i get the length?