从 flutter 应用程序中的 fireStore 获取带有索引的列表

2024-03-23

我正在尝试从 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']
))

但我想获取所有项目列表而不仅仅是一个项目,因此这里索引的方法我认为这是一个错误。


我已经删除了第一个答案,并添加了一个新答案(一旦您看到它,将编辑此行)。

所以,你的存在一些问题fetchAndSetList函数,我重写了它:

Future < void > fetchAndSetList() async {
    final List < RestaurantFoodList > loadedList = [];
    final List < RestaurantitemList > itemList = [];

    await Firestore.instance.collection("restaurant").getDocuments().then(
        (QuerySnapshot snapshot) => snapshot.documents.forEach((f) => {
            itemList = [];
            f.data['items'].forEach((i) =>
                itemList.add(RestaurantitemList(
                    id: i.id,
                    title: i.title,
                    imageUrl i.imageUrl
                    // you might need to add all fields here, even if they are null
                )
            ); 
            loadedList.add(RestaurantFoodList(
                id: f.data['id'],
                imageUrl: f.data['imageUrl'],
                title: f.data['title'],
                items: itemList
            ))
        });
    );

    notifyListeners();

}

我改变了什么?

  • 添加了itemList变量,一个列表RestaurantitemList您将添加到您的最终结果中loadedList;
  • 添加到原始的 forEach 中,第二个 forEach 会用您的值填充 itemListRestaurantitemList对象,我还添加了一条注释,指出您应该添加已映射到的所有字段RestaurantitemList object;
  • 我改变了loadedList.insert to loadedList.add,因此您不必担心记录被添加到什么位置。

NOTE:这尚未经过测试,但应该可以解决遇到的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 flutter 应用程序中的 fireStore 获取带有索引的列表 的相关文章

随机推荐