Firestore数据建模问题



我是firestore的新手,我还在努力理解它。我用flutter/dart做了一个应用程序,却发现我对数据建模的方式:

class1.[class1_index].class2.[class2_index].class3.[class3_index] etc...

这在firestore中不可能。

我一直在尝试找出替代方案,我想从只有一个集合开始,并在该集合中的文档内部嵌套所有数据,而不是创建子集合。

注意:我正在使用json_serializable, annotation和build_runner依赖关系,并相应地注释了模型类

类对象的映射

到目前为止,我已经尝试了嵌套对象映射(而不是对象列表,这是不支持的)作为替代方案,我也遇到了一些问题。

尝试#1 - '无效参数类型,Class2'实例

我在这里遇到的问题是,当我尝试创建第二级对象class1[key].createClass2Object(),然后doc.set(class1[key].toJson())时,我得到错误'无效参数类型,Block'的实例':

void createBlock(String block_name, int clientIndex) async {
    blocks[block_name] = Block(name: block_name);
    blckLst.add(blocks.keys.last);
    await FirebaseFirestore.instance //error here
        .collection('users')
        .doc(AuthService().currentUser?.uid)
        .collection('clients')
        .doc(docID)
        .set(clients[currentClient].toJson());
  }

尝试#2 - 'NoSuchMethod no instance getter'

由于firestore在将文档设置为Client时似乎不喜欢'Block' (Class2/second level的实例)。在实例化时,我尝试将'Block'转换为json:

void createBlock(String block_name, int clientIndex) async {
        blocks[block_name] = Block(name: block_name).toJson();
        blckLst.add(blocks.keys.last);
        await FirebaseFirestore.instance
            .collection('users')
            .doc(AuthService().currentUser?.uid)
            .collection('clients')
            .doc(docID)
            .set(clients[currentClient].toJson());
      }

现在这得到了我的第二级对象映射到firestore,然而,由于'Block'被实例化为Json,我无法访问他们的属性和方法(第三级对象,名称属性等),我得到'NoSuchMethod没有实例getter':

return ExpansionTile(
                          collapsedBackgroundColor: background_color1,
                          backgroundColor: background_color1,
                          title: Center(
                              child: Text(
                            clients[currentClient]
                                .blocks[currentBlock]
                                .name, //error here
                            style: TextStyle(color: fontcolor),
                          )),
                          children: [
                            clients[currentClient]
                                        .blocks[currentBlock]
                                        .mesocycles //error here
                                        .length >
                                    0
                                ? ListView.builder(
                                    shrinkWrap: true,
                                    itemCount: clients[currentClient]
                                        .blocks[currentBlock]
                                        .mesocycles //error here
                                        .length,
                                    itemBuilder: (context, index2) {
                                      return Container(

我已经被困在这个阶段,试图让我的应用程序与firestore工作了一个星期,同时它只花了我几周的时间来制作实际的应用程序功能,所以我已经准备好继续前进,我很沮丧,如此接近有一个工作原型,但我需要一些指导.

如果你对我的数据建模问题有任何建议或解决方案,请在评论中分享或作为答案。

我在第2次尝试中得到的错误上面的例子,我们使用:

blocks[currentBlock].mesocycles.length

代替:

blocks[currentBlock]['mesocycles'].length

由于每个'Block'在实例化时被转换为json,因此我需要这样访问它。

我仍然有点不知道我应该如何构建这个,使它尽可能简单地与Firestore一起使用。

我正在考虑只是制作映射键字符串,从构建器小部件索引转换,所以我可以通过复合索引访问嵌套的数据,就像我最初打算用我最初的"嵌套数组"方法。

最新更新