Search code examples
flutterflutter-layoutflutter-dependenciesflutter-webflutter-test

Length of snapshot for List View builder in flutter[Fixed]


I'm need implement a ListView on Flutter, and i'm passing snapshot.data.length as a parameter for itemCount

return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(                            
snapshot.data[index].data["Identificacao"],...

Then i got an error:

I/flutter ( 4647): Class 'List<DocumentSnapshot>' has no instance getter 'length'.
I/flutter ( 4647): Receiver: Instance(length:1) of '_GrowableList'
I/flutter ( 4647): Tried calling: length

Solution

  • it's easy to solve

    solved the problem by replacing StreamBuilder<Object> with StreamBuilder<QuerySnapshot>. by 
    default the StreamBuilder comes in this form StreamBuilder<Object>
    

    here is an example

    StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance
              .collection('chats/hgjgjgjGW/messages')
              .snapshots(),
          builder: (cnxt, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (ctx, i) => Container(
                  padding: EdgeInsets.all(10),
                  child: Text(snapshot.data!.docs[i]['text']),
                ),
              );
            }
            return Center(
              child: Text('error'),
            );
          }),