我正在尝试从 firestore 获取列表,但我遇到了索引问题。
这是列表上普通项目的结构,每个对象都包含一个项目列表:
class RestaurantFoodList {
final String id ;
final String title;
List <RestaurantitemList> items ;
final String imageUrl;
RestaurantFoodList({this.id,this.title,this.items,this.imageUrl});
}
这是项目本身的结构:
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});
}
这就是我在 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 https://i.stack.imgur.com/mYZZi.png是我的 firestore 数据,我的问题是如何正确获取所有列表及其项目,我尝试了此操作,但它不起作用:
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;
}
注意:如果我编写另一种方法来获取项目,我必须编写
items: loadedItem.add(RestaurantitemList(
id : f.data['items'][0]['id']
))
但我想获取所有项目列表而不仅仅是一个项目,因此这里索引的方法我认为这是一个错误。