简体   繁体   English

Flutter cloud firestore 读取数据参考

[英]Flutter cloud firestore read data with reference

Firestore collection chat has a field user_id it's type is reference and it refers to users collection document id. Firestore 集合聊天有一个字段 user_id,它的类型是引用,它指的是用户集合文档 ID。 I want to use this reference to get user information from user collection like join in sql.我想使用此参考从用户集合中获取用户信息,例如加入 sql。

What I'm getting in my app.我在我的应用程序中得到了什么。

{message: Hello Zia, user_id: DocumentReference(users/mfzfwKvXo5M5dFFuqKeM), event_id: DocumentReference(events/J3HTFRpL0HEbmMV2BSSb), time: Timestamp(seconds=1618466409, nanoseconds=0)}

My Code我的代码

ListView(
              children: snapshot.data.docs.map((DocumentSnapshot document) {
                print(document.data()['user_id']);
                try {
                  print(document.data()['user_id'].toString());
                } catch (e) {}
                return new ListTile(
                  leading: FutureBuilder<DocumentSnapshot>(
                    future: user.doc(**document.data()['user_id']**).get(),   
  // document.data()['user_id])  it's a DocumentReference.
                      builder: (BuildContext context,
                        AsyncSnapshot<DocumentSnapshot> snapshot) {
                      if (snapshot.hasError) {
                        return Text("Something went wrong");
                      }

                      if (snapshot.connectionState == ConnectionState.done) {
                        Map<String, dynamic> data = snapshot.data.data();
                        return Text(
                            "Full Name: ${data['full_name']} ${data['last_name']}");
                      }

                      return Text("loading");
                    },
                  ),
                  subtitle: new Text(document.data()['message']),
                  // subtitle: Text(document.data()['time']),
                );
              }).toList(),
            );

document.data()['user_id]) type 'DocumentReference' is not a subtype of type 'String' document.data()['user_id]) 类型“DocumentReference”不是“String”类型的子类型

I got the solution I'm posting solution it may helps others我得到了我正在发布的解决方案它可能对其他人有帮助的解决方案

document.data()['user_id].path document.data()['user_id].path

path properties return the path of the document path属性返回文档的路径

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

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