Search code examples
firebasefluttergoogle-cloud-firestoreflutter-dependenciesstream-builder

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


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'),
                    ),
                  ),
      ),

Solution

  • 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