Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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,我正在尝试获取存储在用户集合中的当前已验证用户的角色。我试图实现的是在登录时,通过遍历获取集合中用户的文档并筛选字段或检查所有文档并将字段role作为字符串返回来查询用户角色 收集和文档快照(请原谅术语): 目前,users集合中的所有文档都有相同的字段 请问我如何在flifter中编写这种类型的查询?我已尝试在我的服务中使用AuthResult和FirebaseAuth获取当前用户(但无法访问文档中的字段)。 谢谢 字符串角色; getUserRoleWithFuture()异步{ 字符串c

我正在尝试获取存储在
用户集合
中的当前已验证用户的角色。我试图实现的是在登录时,通过遍历获取集合中用户的文档并筛选字段或检查所有文档并将字段
role
作为字符串返回来查询用户角色

收集和文档快照(请原谅术语):

目前,
users集合
中的所有文档都有相同的字段

请问我如何在flifter中编写这种类型的查询?我已尝试在我的服务中使用
AuthResult
FirebaseAuth
获取当前用户(但无法访问文档中的字段)。 谢谢

字符串角色;
getUserRoleWithFuture()异步{
字符串currID=await_authService.getCurrentUID();
串孔;
Firestore.instance.collection(USERS\u REF).document(currID).get().then((doc){
mRole=doc.data['role'];
打印(mRole);
});
返回mRole;
}
未来的getUserRoleWithStream()异步{
字符串currID=await_authService.getCurrentUID();
字符串sRole;
Firestore.instance
.收集(用户参考)
.文件(currID)
.快照()
.listen((文档快照){
如果(ds.exists){
sRole=ds.data['role'];
打印('with stream:\t$sRole');
}
});
返回sRole;
}
在方法
getUserRoleWithStream()
中,我试图检索打印出来的值,就像
role=getUserRoleWithStream()
一样,但是在控制台中获取该值
不能将Future类型的值分配给string类型的变量

如何使用流(因为它不断观察集合)或使用其他方法获取该值并在我的小部件中使用它?
再次感谢。

首先,我假设
AuthResult.user.uid
和您的用户集合用户id相同。因此,一旦您从
AuthResult
获得了用户,您就可以查询firestore集合以获得用户角色,如下所示

Future<String> getUserRole(String uid) async {
    DocumentSnapshot ds = await Firestore.instance.collection('users').document(uid).get();
    return ds.data['role'];
  }
未来getUserRole(字符串uid)异步{ DocumentSnapshot ds=等待Firestore.instance.collection('users').document(uid.get(); 返回ds.data['role']; }
首先,我假设
AuthResult.user.uid
和您的用户集合用户id相同。因此,一旦您从
AuthResult
获得了用户,您就可以查询firestore集合以获得用户角色,如下所示

Future<String> getUserRole(String uid) async {
    DocumentSnapshot ds = await Firestore.instance.collection('users').document(uid).get();
    return ds.data['role'];
  }
未来getUserRole(字符串uid)异步{ DocumentSnapshot ds=等待Firestore.instance.collection('users').document(uid.get(); 返回ds.data['role']; }
这是有效的解决方案,以防其他人遇到这种情况。我感谢您为帮助我理解这个问题所做的努力,但以下是答案:

String role;

getUserRoleWithFuture() async {
  String currID = await _authService.getCurrentUID();
  String mRole;
  Firestore.instance.collection(USERS_REF).document(currID).get().then((doc) {
    mRole = doc.data['role'];
    print(mRole);
   });
   return mRole;
 }

Future<String> getUserRoleWithStream() async {
  String currID = await _authService.getCurrentUID();
  String sRole;
  Firestore.instance
    .collection(USERS_REF)
    .document(currID)
    .snapshots()
    .listen((DocumentSnapshot ds) {
     if (ds.exists) {
       sRole = ds.data['role'];
       print('with stream:\t$sRole');
     }
   });
   return sRole;
 }
字符串角色;
getUserRoleWithFuture()异步{
字符串currID=await_authService.getCurrentUID();
串孔;
Firestore.instance.collection(USERS\u REF).document(currID).get().then((doc){
mRole=doc.data['role'];
打印(mRole);
});
返回mRole;
}
未来的getUserRoleWithStream()异步{
字符串currID=await_authService.getCurrentUID();
字符串sRole;
Firestore.instance
.收集(用户参考)
.文件(currID)
.快照()
.listen((文档快照){
如果(ds.exists){
sRole=ds.data['role'];
打印('with stream:\t$sRole');
}
});
返回sRole;
}

这是有效的解决方案,以防其他人遇到这种情况。我感谢您为帮助我理解这个问题所做的努力,但以下是答案:

String role;

getUserRoleWithFuture() async {
  String currID = await _authService.getCurrentUID();
  String mRole;
  Firestore.instance.collection(USERS_REF).document(currID).get().then((doc) {
    mRole = doc.data['role'];
    print(mRole);
   });
   return mRole;
 }

Future<String> getUserRoleWithStream() async {
  String currID = await _authService.getCurrentUID();
  String sRole;
  Firestore.instance
    .collection(USERS_REF)
    .document(currID)
    .snapshots()
    .listen((DocumentSnapshot ds) {
     if (ds.exists) {
       sRole = ds.data['role'];
       print('with stream:\t$sRole');
     }
   });
   return sRole;
 }
字符串角色;
getUserRoleWithFuture()异步{
字符串currID=await_authService.getCurrentUID();
串孔;
Firestore.instance.collection(USERS\u REF).document(currID).get().then((doc){
mRole=doc.data['role'];
打印(mRole);
});
返回mRole;
}
未来的getUserRoleWithStream()异步{
字符串currID=await_authService.getCurrentUID();
字符串sRole;
Firestore.instance
.收集(用户参考)
.文件(currID)
.快照()
.listen((文档快照){
如果(ds.exists){
sRole=ds.data['role'];
打印('with stream:\t$sRole');
}
});
返回sRole;
}

显示您当前的代码,您做得怎么样?再次检查,之前没有尝试过查询显示您当前的代码,您做得怎么样?再次检查,之前没有尝试过查询这是错误跟踪:“Future”E/flatter(32759)的实例:[错误:flatter/lib/ui/ui\u dart\u state.cc(157)]未处理的异常:NoSuchMethodError:方法“[]'被调用为空。E/flatter(32759):接收者:null E/flatter(32759):尝试调用:[”(“角色”)您的代码在逻辑上似乎正确,但由于某种原因不起作用,正如我所说的,在Firestore中创建用户文档时,uid和文档id是否相同??是的,它们是使用id查询字段的相同方法,它现在可以工作,但无法返回我想要的字符串这是错误跟踪:“Future”E/flatter(32759)的实例:[error:flatter/lib/ui/ui\u dart\u state.cc(157)]未处理的异常:NoSuchMethodError:方法“[]”在null上被调用。E/flatter(32759):接收者:null E/flatter(32759):尝试调用:[”(“角色”)您的代码在逻辑上似乎正确,但由于某些原因不起作用,正如我所说的,在Firestore中创建用户文档时,uid和文档id是否相同??是的,它们是使用id查询字段的相同方法,它现在可以工作,但无法返回我想要的字符串