简体   繁体   English

如何从refrenced文档(Firebase)中的字段获取String数据

[英]How to get String data from field inside a refrenced document (Firebase)

In my Flutter Application I would like to get some information from a field which contains a refrence to another Collection. 在我的Flutter应用程序中,我想从一个包含对另一个Collection的参考的字段中获取一些信息。 I don't know how to get to the information from the field inside the referenced document. 我不知道如何从引用文档中的字段获取信息。

Down below are two pictures of both collections. 下面是这两个系列的两张照片。

The first picture contains the main collection where the field ("geslacht") refers to: "/gender/Girl" 第一张图片包含该字段(“geslacht”)所指的主要集合:“/ gender / Girl”

收集1

The second picture shows the referenced collection. 第二张图显示了引用的集合。

收集2

I currently have written the following piece (following this tutorial ) 我目前写了以下文章(按照本教程

class Record {
  final String name;
  var color;
  final DocumentReference reference;
  Record.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['name'] != null),
        assert(map['geslacht'] != null),
        name = map['name'],
        color = map['geslacht'];
  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);  
  @override
  String toString() => "Record<$name:$votes>";
}

Which perfectly gets the data from the field called "name" and returns a instance of DocumentReference for the field called "geslacht" 哪个完美地从名为“name”的字段中获取数据,并为名为“geslacht”的字段返回DocumentReference的实例

I would love to get the information in this referenced document. 我很想获得此参考文档中的信息。 So to conclude the value "pink" is what I am trying a get. 因此,总结价值“粉红色”是我想要得到的。 (The path would be baby --> dana --> geslacht --> gender --> girl --> color --> pink ) (路径是宝贝 - > dana - > geslacht - >性别 - >女孩 - >颜色 - >粉红色)

Thanks in advance for the help! 先谢谢您的帮助!

You will have to get() the other document using that DocumentReference, just like you would if you built the DocumentReference yourself. 您必须使用该DocumentReference get()另一个文档,就像您自己构建DocumentReference一样。 Cloud Firestore will not automatically follow references for you - you have to write code for that. Cloud Firestore不会自动跟踪您的引用 - 您必须为此编写代码。

You need to query for your data using that reference, you currently only grab that first document: 您需要使用该引用查询数据,您目前只获取第一个文档:

class Record {
  final String name;
  var color;
  final DocumentReference reference;
  Record.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['name'] != null),
        assert(map['geslacht'] != null),
        name = map['name'],
        color = map['geslacht'];
  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);  
  @override
  String toString() => "Record<$name:$votes>";
}

What you need to do is to perform another query like this: 你需要做的是执行另一个这样的查询:

_getGender(ref) async {
  // ref is '/gender/Girl/' in your case
  var query = await Firestore.instance.document(ref).get();
  print('color is: ' + query.data['color'])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM