【问题标题】:Flutter cloud firestore read data with referenceFlutter Cloud Firestore 读取数据有参考
【发布时间】:2021-07-10 13:50:36
【问题描述】:

Firestore 集合聊天有一个字段 user_id,它的类型是引用,它指的是用户集合文档 ID。我想使用这个引用从用户集合中获取用户信息,比如加入 sql。

我在我的应用中得到了什么。

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

我的代码

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' 不是 type 'String' 的子类型

【问题讨论】:

    标签: firebase flutter google-cloud-firestore flutter-dependencies


    【解决方案1】:

    我得到了我正在发布的解决方案它可能对其他人有帮助的解决方案

    document.data()['user_id].path

    path属性返回文档的路径

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-25
      • 2018-10-26
      • 1970-01-01
      • 2018-08-02
      • 2022-06-24
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多