0

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

But these sintax are used in many tutorials i have seen. I had tried use:

snapshot.data.documents.length;

but the result is the same. Please help me!

  • Was this copy + pasted or is `lenght` misspelled in your source (twice)? edit: looking again, looks like a typo based on the stacktrace. – Phix Oct 23 '18 at 01:46
  • I actually wrote the wrong question here. But there is not even a suggestion (Ctrl + space bar). Because it is a list, I imagine there must be a way to know the size so that you can walk through it with a loop, etc ... – Rafael Pablo Massocato Oct 23 '18 at 01:56
  • 3
    Almost a week and no response, wow ... I'm glad I chose a popular language maintained by Google to develop my first app – Rafael Pablo Massocato Oct 29 '18 at 02:31
  • Lashing out aside, what is `snapshot`? What tutorials are you following? – Phix Oct 29 '18 at 02:36

7 Answers7

3

If you are working with StreamBuilder, the way to find the length of the data isn't like that.

snapshot.data.length

will not work since your asking the length for the Instance of the snapshot, So you will have No Such method or No such class errors

So what you should do is.

snapshot.data.snapshot.value.length

Let me show you an example

StreamBuilder(
    stream: FirebaseDatabase.instance
              .reference()
              .child("users")
              .orderByChild('firstName')
              .limitToFirst(20)
              .onValue,
    builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: snapshot.data.snapshot.value.lenght,//Here you can see that I will get the count of my data
                  itemBuilder: (context, int) {
                  //perform the task you want to do here
                    return Text("Item count ${int}");
                  });
            } else {
              return Container();
            }
      },
  ),

you can also take a look at this answer on what is the difference between stream and futurbuilder https://stackoverflow.com/a/50844913/9949983

Abel Tilahun
  • 1,705
  • 1
  • 16
  • 25
3

solved the problem by replacing

StreamBuilder<Object> 

with

StreamBuilder<QuerySnapshot> 

by default the StreamBuilder comes in this form StreamBuilder

this will work 100%

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
MhdBasilE
  • 356
  • 1
  • 4
  • 13
1

Replace :

builder: (context, snapshot) {}

with :

builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {}

Notice: use your type instead of List<dynamic>

Text() class only accepts string value if your type isn't string use toString() method :

Text(snapshot.data![index].toString())

overall :

StreamBuilder(
      stream: bloc.samples,
      builder: (context, AsyncSnapshot<List<int>> snapshot) {
        if (snapshot.hasData) {
          return ListView.builder(
            itemCount: snapshot.data!.length,
            itemBuilder: (context, int index) {
              return Text(snapshot.data![index].toString());
            },
          );
        }
      },
    )
Umamad
  • 551
  • 4
  • 10
0

I used this to map and increment to get the length of the snapshot:

int snapshotLength = 0;

snapshot.data.documents.map<YourGenericType>((document){
 snapshotLength++;
 print("Snapshot length: $snapshotLength");
}
Oush
  • 3,090
  • 23
  • 22
0

Using StreamBuilder widget in flutter.

StreamBuilder(
        stream: Firestore.instance
                .collection('followers')
                .document(<DocID>)
                .collection('<COLLECTION>')
                .snapshots(),
        builder: (context, snapshot) {
                QuerySnapshot values = snapshot.data;
                print(values.length);
       },
);
Steven Ogwal
  • 685
  • 6
  • 8
0

only try: snapshot.data.documents.length not suggest in vscode ide dart plugin but it will work.

-2

maybe snapshot.documents.length ?

bobbyrne01
  • 6,295
  • 19
  • 80
  • 150