在带有索引的flutter应用程序中从fireStore获取列表 [英] get list from fireStore in flutter app with indexes

查看:77
本文介绍了在带有索引的flutter应用程序中从fireStore获取列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firestore获取列表,但索引存在问题.

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}); 
}

这是物品本身的结构:

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中添加对象的方式:


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,
            }
         ],
    });
} 

是我的Firestore数据,我的问题是如何获取全部列表及其项目正确无误,我尝试了此操作,但没有成功:

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.

推荐答案

我已经删除了第一个答案,并添加了一个新答案(一旦看到该行,就会对其进行编辑).

I have deleted the first answer and I am adding a new one (will edit this line out once you see it).

因此,您的 fetchAndSetList 函数存在一些问题,我将其重写:

So, there are a few problems with your fetchAndSetList function, and I rewrote it:

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,其中用您的 RestaurantitemList 对象填充itemList,我还添加了一条注释,指出您应该添加已映射到 RestaurantitemList上的所有字段对象;
  • 我将 loadedList.insert 更改为 loadedList.add ,因此您不必担心将记录添加到哪个位置.
  • Added the itemList variable, a list of RestaurantitemList that you will add to your final loadedList;
  • Added to the original forEach a second forEach that populates the itemList with your RestaurantitemList objects, I also added a comment stating that you should add all the fields that you have mapped on the RestaurantitemList object;
  • I changed the loadedList.insert to loadedList.add, so you don't have to worry about what position the record is being added to.

注意:未经测试,但这应该可以解决存在的问题.

NOTE: this is untested, but it should fix the issues that were having.

这篇关于在带有索引的flutter应用程序中从fireStore获取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆