I have been following a tutorial on creating a food ordering app.
In the tutorial, there is a restaurant model file that includes the the menu items.
` final List _menu = [
Food(
name: 'Classic Burger',
description: 'A half pounder with all the fixins.',
imagePath: 'lib/images/burgers/burger1.jpg',
price: 10.99,
category: FoodCategory.burgers,
availableAddons: [
Addon(name: 'Extra cheese', price: 0.69),
Addon(name: 'Bacon', price: 1.09),
Addon(name: 'Avocado', price: 2.25),
],
),`
then the getter is
List<Food> get menu => _menu;
I would like to get this data from firestore.
I have created a collection named ‘menu’
enter image description here
I have created this future method in the same dart file.
Future<List> getMenuData() async {
CollectionReference menu = FirebaseFirestore.instance.collection(‘menu’);
// Get docs from collection reference
// QuerySnapshot querySnapshot = menu.get() as QuerySnapshot<Object?>;
QuerySnapshot querySnapshot = await menu.get();
// Get data from docs and convert map to List
final allData = querySnapshot.docs.map((doc) => doc.data()).toList();
return allData as List<Food>;
}
I then updated the getter to
List<Food> get menu => getMenuData();
This generated compile error
A value of type ‘Future<List>’ can’t be returned from the function ‘menu’ because it has a return type of ‘List’.
So how can I get the menu loaded from firestore instead of hard coded in the model file?
Ken Roberts is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.