检索Firebase集合中的所有文档(一个文档除外)

时间:2020-06-11 19:03:58

标签: firebase flutter google-cloud-firestore

我正在尝试具有一个集合引用,并且仅排除一个具有moduleId ==当前用户模块ID字段的引用。我写了这个函数来获取模块:

Future<void> get_other_modules() async {
    final DocumentSnapshot userSnapshot = (await Firestore.instance
            .collection('users')
            .where('uid',
                isEqualTo: (await FirebaseAuth.instance.currentUser()).uid)
            .getDocuments())
        .documents[0];

    //return recommended_module;
    setState(() {
      other_modules = Firestore.instance
          .collection('modules')
          .where("moduleID",isGreaterThan: userSnapshot.data['moduleID'])         
      .where('moduleID', isLessThan: userSnapshot.data['moduleID']);
    });
  }

我在初始化状态下调用此函数,然后尝试通过流生成器在屏幕上查看它,但是,我得到了在null上调用快照的错误。有什么主意吗?

new StreamBuilder<QuerySnapshot>(

                    stream: other_modules.snapshots(),
                    builder: (BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (snapshot.hasError)
                        return new Text('${snapshot.error}');

                      if (snapshot.hasError) {
                        print("error");
                        return new Text('${snapshot.error}');
                      }
                      if (!snapshot.hasData)
                        return Center(
                            child: CircularProgressIndicator(
                          backgroundColor: Colors.deepPurple,
                        ));
                      return new ListView(
                          shrinkWrap: true, children: getModules(snapshot));
                      ;
                    })

函数getModules用于检索文档,这里是:

  getModules(AsyncSnapshot<QuerySnapshot> snapshot) {
    return snapshot.data.documents
        .map(
          (item) => Card(
            child: Column(
              children: <Widget>[
                SingleChildScrollView(
                    scrollDirection: Axis.vertical,
                    child: Column(
                        //shrinkWrap: true,
                        children: <Widget>[
                          ListTile(
                              title: new Text(item['moduleName']),
                              subtitle: new Text(item['moduleDesc'])),
                          getTags(item['moduleTarget'])
                        ])),
              ],
            ),
          ),
        )
        .toList();
  }

更新我用属性引用更新了其他模块查询,如下所示:

setState(() {
      other_modules = Firestore.instance
          .collection('modules')
          .where("uid", isGreaterThan: userSnapshot.data['moduleID'])
          .where('moduleID', isLessThan: userSnapshot.data['moduleID'])
          .reference();
    });

但是,我拥有所有的模块,仍然不排除用户的模块吗?

0 个答案:

没有答案