Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter Flatter从Firestore检索文档数据_Flutter_Google Cloud Firestore - Fatal编程技术网

Flutter Flatter从Firestore检索文档数据

Flutter Flatter从Firestore检索文档数据,flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,我正在尝试从当前uid的文档中检索数据“name”和“email”。然而,我得到的数据是参考路径。有人能给我指出正确的方向吗?谢谢 现在,您只需引用文档路径即可。您需要对该文档进行查询 现在你有: var userDetail = FirebaseFirestore.instance.collection('users').doc(user.uid); 将此更改为: FirebaseFirestore.instance.collection('users').doc(user.uid)

我正在尝试从当前uid的文档中检索数据“name”和“email”。然而,我得到的数据是参考路径。有人能给我指出正确的方向吗?谢谢


现在,您只需引用文档路径即可。您需要对该文档进行查询

现在你有:

var userDetail = FirebaseFirestore.instance.collection('users').doc(user.uid);
将此更改为:

FirebaseFirestore.instance.collection('users').doc(user.uid)
    .get()
    .then((DocumentSnapshot userDetail) {
      if (userDetail.exists) {
        name = userDetail.data()['name'];
        email = userDetai.data()['email'];
      }
    });
引用:

CollectionReference users=FirebaseFirestore.instance.collection('users');
回归未来建设者(
future:users.doc(USERID.get(),
建设者:
(BuildContext上下文,异步快照){
if(snapshot.hasError){
返回文本(“出错”);
}
if(snapshot.connectionState==connectionState.done){
映射数据=snapshot.data.data();
返回文本(“全名:${data['Name']}${data['email']}”);
}
返回文本(“加载”);
},
);

看看文档,您正在创建一个DocumentReference变量,而不是DocumentSnapshot

你好
代码中的错误是您没有使用
DocumentReference
上的方法
get()
。 下面是你的工作代码

DocumentSnapshot userDetail = await Firestore.instance.collection('users').doc(user.uid).get();
var name = userDetail.data()['name'];
var email = userDetail.data()['email'];


“名称”和“电子邮件”显示为空。这是来自控制台的代码:访问隐藏方法Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object;(greylist,linking,allowed)访问隐藏方法Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J(greylist,核心平台api,linking,allowed)控制台返回“访问隐藏方法Lsun/misc/Unsafe;->”getObject(Ljava/lang/Object;J)Ljava/lang/Object;(灰色列表,链接,允许)“”。名称显示为“Null”,但在我刷新页面后,会显示正确的名称和电子邮件。有没有办法解决这个问题?@Roy,我想你在检索数据后没有使用
setState()
。使用
setState()
,您可以在数据更改后刷新应用程序的UI。谢谢!现在可以用了
DocumentSnapshot userDetail = await Firestore.instance.collection('users').doc(user.uid).get();
var name = userDetail.data()['name'];
var email = userDetail.data()['email'];
var userDetail;
Firestore.instance.collection('users').doc(user.uid).get().then((DocumentSnapshot doc) {
   userDetail = doc;
   var name = userDetail.data()['name'];
   var email = userDetail.data()['email'];
});