【问题标题】:Adding collection and subcollection data to model entity from firestore firebase将集合和子集合数据添加到来自 Firestore Firebase 的模型实体
【发布时间】:2021-01-11 05:59:02
【问题描述】:

我最近开始使用 Firebase Firestore。如果集合有子集合并将这些项目添加到我的模型中,我就会陷入困境。

假设我有这个收藏

meats
  \ id : 1
  \ name : Chicken
 subMeats 
      \ id : 11
      \ name : Country Chicken
      \ id : 12
      \ name : Broiler Chicken
meats 
  \ id : 2
  \ name : Pork
subMeats
      \ id : 21  
      \ name : White Pork
      \ id : 22
      \ name : Black Pork

     return meatTypeCollection.get().then((value) {
  value.docs.forEach((mainDoc) {
   // MainMeatEntity.fromSnapshot(mainDoc)    
    _logger.i(mainDoc.data()['name']);
    meatTypeCollection
        .doc(mainDoc.id)
        .collection('subMeats')
        .get()
        .then((snapShots) {
      snapShots.docs.forEach((doc) {
        _logger.i(doc.data()['name']);
        MainMeat.fromEntity(MainMeatEntity.fromSnapshot(doc));
      });
    });
  });
  //return MeatTypes.fromEntity(MeatTypeEntity.fromJson(value.docs));
}).catchError((error) => _logger.e("Failed to load meat types : $error"));

上面没有捕获集合。我使用实体建模。

   import 'package:flutter/cupertino.dart';
   import 'entities/main_meat_entity.dart';
   import 'entities/meat_type_entity.dart';

    @immutable
    class MainMeat {
    final String id;
    final String name;
    final MeatTypeEntity subMeats;
    final String shopId;

 const MainMeat({
   this.id,
   this.name,
   this.subMeats,
   this.shopId,
 });

 static MainMeat fromEntity(MainMeatEntity mainMeatEntity) {
   return MainMeat(
    id: mainMeatEntity.id,
    name: mainMeatEntity.name,
    subMeats: mainMeatEntity.subMeats,
    shopId: mainMeatEntity.shopId,
   );
  }

  MainMeatEntity toEntity() {
     return MainMeatEntity(id, name, subMeats, shopId);
  }

  static const empty = MainMeat(id: '', shopId: "", name: "");
   }
  -----------------------------****************--------------------
  part 'meat_type_entity.g.dart';

 @JsonSerializable()
 class MeatTypeEntity extends Equatable {
  final String id;
  final String name;
  final String shopId;

  const MeatTypeEntity(this.id, this.name, this.shopId);

  factory MeatTypeEntity.fromJson(Map<String, dynamic> json) =>
  _$MeatTypeEntityFromJson(json);

   Map<String, dynamic> toJson() => _$MeatTypeEntityToJson(this);

   @override
   List<Object> get props => [
     id,
     name,
     shopId,
    ];

   static MeatTypeEntity fromSnapshot(DocumentSnapshot snap) {
    return MeatTypeEntity(
    snap.id,
    snap.data()['name'],
    snap.data()["shopId"],
      );
     }

    Map<String, Object> toDocument() {
     return {
     "id": id,
     'mainMeat': name,
     "shopId": shopId,
     };
    }
   }

我可以列出它的收藏和子收藏。但不确定加载到我的模型中。任何帮助表示感谢。

【问题讨论】:

  • 嗨!我不太明白你在这里的斗争是什么。你的意思是你不知道如何将 firestore 快照转换为 Dart 类?
  • 是的,在我的模型中难以构建
  • 你必须为两者使用不同的模型。
  • 您是否尝试过编写任何代码?如果有,可以分享一下吗?
  • 是的,我做到了,但很难做到。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

您不能将 subCollection 直接存储到集合中,ReadFireStore 数据模型部分。

所以你的结构看起来像 Meal(文档) -> 所有餐点集合 -> Sub Meal (文档) -> 所有子餐点集合。

如果你想读取像 firebase 数据库这样的数据,你不能一次读取完整的树/层次结构。

【讨论】:

    【解决方案2】:

    所以假设这是一个关于如何创建模型的 Dart 问题,我会创建如下内容:

    class Meat {
      final String id;
      final String name;
      final List<SubMeat> subMeats;
    
      Meat({
        @required this.id,
        @required this.name,
        this.subMeats,
      });
    }
    
    class SubMeat {
      final String id;
      final String name;
    
      SubMeat({
        @required this.id,
        @required this.name,
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2021-11-30
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多