【问题标题】:how to search for a document in firestore by its reference如何通过引用在 Firestore 中搜索文档
【发布时间】:2020-02-03 11:43:37
【问题描述】:

我有两个集合,一个是普通集合,另一个是第一个的 documentReference ,我想要做的是通过它的引用给我具体的文档, 我通过在 for 循环中搜索来找到特定的文档,但是有没有更好的方法来做到这一点,而不是从第一个集合中获取所有文档,只需获取我需要的唯一一个。

static Future<List<author_model>> getTHEauthor(DocumentReference documentReference) async{
    String ref = documentReference.documentID;
    DocumentReference dReference = articleCollection.document(documentReference.documentID);
    List<author_model> author=[];
    author_model model ;
    final QuerySnapshot querySnapshot = await authors.getDocuments();
    List<DocumentSnapshot> result = querySnapshot.documents;

    for(int i=0 ; i<result.length ; i++) {
      if (result[i].documentID == ref) {
          model = new author_model(
           result[i]['author_name'], result[i]['author_image'],
           result[i]['author_work_exeperience']
           , result[i]['autor_education']);
           author.add(model);
         break;
      }
    }
    // print(author.length);
    return author;
  }

我也试过这个,但什么也没给我

var snap = documentReference.get();
author_model m = author_model.map(snap);
print(m.author_name + ": " + m.author_education)

也试过这个查询

 final QuerySnapshot querySnapshot = await authors.where('documentID', isEqualTo: 
  ref).getDocuments();

但没有结果,我不知道如何仅获取具有特定参考的文档。 我不想获取作者集合中的所有文档,

谁能帮我查询和获取特定文档吗?

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    我建议您看看这里已经回答的问题: Finding all docs with specific reference in Cloud Firestore

    这似乎与您的情况非常相关,并且该解决方案肯定会对您有所帮助。 :)

    如果链接对你有帮助,请告诉我!

    【讨论】:

    • 我找到了解决方案..@gso_gabriel
    • 感谢您的帮助和回复
    【解决方案2】:

    我使用了这段代码,我使用了流生成器@gso_gabriel

    Stream authorStream ;
    
    
    
    @override
    void initState() {
    authorStream=Firestore.instance.collection('doctors_authors').document(_article.author_reference.documentID).snapshots();
    super.initState(); }
    
    
    
      child: StreamBuilder(
           stream: authorStream,
          builder: (context, snapshot) {
           if (snapshot.hasData) {
            return Column(
              children: <Widget>[
                SizedBox(height: 30,),
                GestureDetector(
                  child: CircleAvatar(
                    radius: 50,
                    backgroundImage: NetworkImage(snapshot.data['author_image'].toString()),),
    

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 2018-06-21
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多