How do I get some FireBase data into a ListView?
The following code successfully returns a list of products from FireBase:
final CollectionReference _products = FirebaseFirestore.instance.collection('products');
Future<List> getList() async {
QuerySnapshot querySnapshot = await _products.get();
final temp = querySnapshot.docs.map((doc) => doc.data()).toList();
return temp;
}
The returned data looks like this:
[{quantity: 1.0, name: Greek Yogurt 🇬🇷, category: Dairy, priority: 0}, {quantity: 1.0, name: Watermelon 🍉, category: Produce, priority: 0}, {quantity: 1.0, name: Cinnamon 🍂, category: Spices, priority: 0}, {quantity: 4.0, name: Banana 🍌, priority: 0, category: Produce}, {quantity: 1.0, name: Heavy Cream 🐄, priority: 0, category: Dairy}]
I can get these Column & Text widgets to display the returned data:
return Column(
children: <Widget>[
const Text("FireBase Data"),
Text("${snapshot.data}"),
Text(snapshot.data?.length.toString() ?? '0'),
]
);
But whenever I try to use a ListView, I get stumped.
Can anyone help me get the returned data into a ListView?
I’m sure it’s relatively simple, I’m just Flutter dense…
Thanks!
2
Answers
I don’t know exactly what you want to show in the ListView but I hope the following code helps you achieve what you want:
You’re very vague about what you want to do with your ListView, but just looking at your code and trying to guess your intentions, I felt that this would make a lot of sense to me:
If this is not what you want, plz tell us what is! 🙂