如何使用flatter和firebase获取与我的userId相关的周围数据
使用flatter时,我能够成功获取如何使用flatter和firebase获取与我的userId相关的周围数据,firebase,flutter,firebase-realtime-database,dart,Firebase,Flutter,Firebase Realtime Database,Dart,使用flatter时,我能够成功获取用户ID,但是我希望能够获取更多用户数据(使用用户ID) 周围信息: 使用userId,我将如何打印用户;姓名、会员资格。。。etc?getUsers()异步{ getUsers() async { //here fbPath is reference to your users path fbPath.once().then((user){ if(user.value !=null){ Map.from(user.value).f
用户ID
,但是我希望能够获取更多用户数据(使用用户ID)
周围信息:
使用userId,我将如何打印用户;姓名、会员资格。。。etc?getUsers()异步{
getUsers() async {
//here fbPath is reference to your users path
fbPath.once().then((user){
if(user.value !=null){
Map.from(user.value).forEach((k,v){
//here users is List<Map>
setState((){
users.add(v);
});
}
}
});
}
//Or
getUsers() async {
//here fbPath is reference to your users path
//and userListener is StreamSubscription
userListener = fbPath.onChildAdded.listen((user){
//here users is List<Map>
setState((){
users.add(Map.from(user.snapshot.value));
});
});
}
//and cancel in dispose method by calling
userListener.cancel();
//这里fbPath是对用户路径的引用
fbPath.once().then((用户){
if(user.value!=null){
Map.from(user.value).forEach((k,v){
//这里是用户列表
设置状态(){
用户。添加(v);
});
}
}
});
}
//或
getUsers()异步{
//这里fbPath是对用户路径的引用
//而userListener是StreamSubscription
userListener=fbPath.onChildaded.listen((用户){
//这里是用户列表
设置状态(){
添加(Map.from(user.snapshot.value));
});
});
}
//并通过调用
userListener.cancel();
试着这样做:
Future<dynamic> getWeightinKeg() async {
final DocumentReference document = Firestore.instance.collection('you_collection_name').document(user_id);
await document.get().then<dynamic>(( DocumentSnapshot snapshot) async {
final dynamic data = snapshot.data;
print(data['name'].toString())
//Do whatever you want to do with data here.
});
}
Future getWeightinKeg()异步{
final DocumentReference document=Firestore.instance.collection('you\u collection\u name')。document(user\u id);
等待document.get()。然后((DocumentSnapshot snapshot)异步{
最终动态数据=snapshot.data;
打印(数据['name'].toString())
//对这里的数据做任何你想做的事情。
});
}
由于您使用的是实时数据库,因此要获取其他数据,您可以执行以下操作:
db = FirebaseDatabase.instance.reference().child("Users");
db.once().then((DataSnapshot snapshot){
Map<dynamic, dynamic> values = snapshot.value;
values.forEach((key,values) {
print(values);
print(values["name"]);
});
});
db=FirebaseDatabase.instance.reference().child(“用户”);
db.once().then((DataSnapshot快照){
映射值=snapshot.value;
values.forEach((键,值){
打印(值);
打印(值[“名称]);
});
});
首先添加对节点
Users
的引用,然后使用forEach
方法在检索到的Map
中迭代并检索其他值。感谢您的反馈。但是,既然您的代码打印了数据库中找到的每个值,我该如何从特定的userId中选取周围的数据呢例如,它输出:{phone:,userId:Rnp06XZAkjZ3fST56z97z7yHKls2,bio:Hello,email:tommy。sarkissian@gmail.com,声誉:2,名称:TEST,成员:PREMIUM}如何打印这个特定的名称,测试?使用用户ID作为参考。