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

标签 firebase flutter dart google-cloud-firestore

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

this是我的 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 会用您的 RestaurantitemList 填充 itemList对象,我还添加了一条注释,指出您应该添加已映射到 RestaurantitemList 上的所有字段。对象;
  • 我更改了loadedList.insertloadedList.add ,因此您不必担心记录被添加到什么位置。

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

关于firebase - 从 flutter 应用程序中的 fireStore 获取带有索引的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60628098/

相关文章:

flutter - 如何在Flutter中显示来自String Url的图像?

json - Flutter 用数组解析 JSON

rest - Dart 将 SSL 证书和 key 添加到 HttpClient

dart - Flutter tween 基本动画在 `FutureBuilder` 内部不起作用

java - firebase 关闭调用关闭连接

javascript - Angularjs 中的分页

java - Android:java.lang.NoSuchMethodError:没有静态方法 zzy。

ios - Firebase iOS/Swift 和深层链接

按下按钮时不显示 flutter 对话框

android - flutter 的 Image.network 无法在发布 apk 上运行