'Length of snapshot for Listview on Flutter
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!
Solution 1:[1]
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
Solution 2:[2]
solved the problem by replacing
StreamBuilder<Object>
with
StreamBuilder<QuerySnapshot>
by default the StreamBuilder comes in this form StreamBuilder
this will work 100%
Solution 3:[3]
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");
}
Solution 4:[4]
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);
},
);
Solution 5:[5]
only try:
snapshot.data.documents.length
not suggest in vscode ide dart plugin but it will work.
Solution 6:[6]
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());
},
);
}
},
)
Solution 7:[7]
maybe snapshot.documents.length ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Slava Rozhnev |
| Solution 3 | Oush |
| Solution 4 | Steven Ogwal |
| Solution 5 | berkaycapar |
| Solution 6 | Umamad |
| Solution 7 | bobbyrne01 |
