I'm trying to get a list from firestore but i had a problem with indexes.
This is the structure of the normal Items on the List and every object contain a list of items:
class RestaurantFoodList {
final String id ;
final String title;
List <RestaurantitemList> items ;
final String imageUrl;
RestaurantFoodList({this.id,this.title,this.items,this.imageUrl});
}
This is the structure of the items themselfs:
class RestaurantitemList {
final String id ;
final String title ;
final String imageUrl ;
final String description ;
final int price ;
final int shippingPrice;
List<Checkbox> cheker;
RestaurantitemList({this.id,this.title,this.imageUrl,this.description,this.price,this.shippingPrice,this.cheker});
}
This is how i add the object in firestore:
Future<void> addLitem(RestaurantFoodList foodList, RestaurantitemList itemList) async {
final _fireStore = Firestore.instance;
_fireStore.collection('restaurant').add({
'title': foodList.title,
'imageUrl': foodList.imageUrl,
'items': [
{
'id': itemList.id,
'title': itemList.title,
'imageUrl': itemList.imageUrl,
}
],
});
}
and this is my firestore data, my problem is how can i fetch All list and their items correctly, i tried this but it didn't work:
Future<void> fetchAndSetList() async {
final List<RestaurantFoodList> loadedList = [];
await Firestore.instance.collection("restaurant").getDocuments().then(
(QuerySnapshot snapshot) => snapshot.documents.forEach(
(f) => loadedList.insert(
0,
RestaurantFoodList(
id: f.data['id'],
imageUrl: f.data['imageUrl'],
title: f.data['title'],
items: [f.data['items']]
),
),
),
);
notifyListeners();
_restaurantItems[3].itemsList = loadedList;
}
note : if i write another method to get Items i must write
items: loadedItem.add(RestaurantitemList(
id : f.data['items'][0]['id']
))
but i want to get all the list of items not just one item so the approach of the index here i think it's a mistake.