从Firebase Datasnapshot获取JSON密钥值 [英] Getting JSON Key Value from Firebase Datasnapshot
问题描述
与此同时,我正在用Firebase开发Flutter应用程序,确实很有趣,当然还有克服的麻烦...
Meanwhile I am developing a Flutter app with Firebase and it is enjoyable indeed, of course there is hassle to overcome...
我设置了Firebase数据存储区,并且通过firebase_database插件,我得到了一个DataSnapshot,返回如下:
I setup a Firebase datastore, and through the firebase_database plugin I got a DataSnapshot returned as follow:
FirebaseDatabase.instance.reference().child('users')
.orderByChild('emailAddress')
.equalTo('wonderjimmy@gmail.com').once()
.then((onValue) {
Map data = onValue.value;
});
我使用Map对象保留此JSON。一个愚蠢的问题:如果我只想获取值 -L474-TYYYGPvCChlZCS ,该怎么办?
I use a Map object to keep this JSON. A stupid question: if I just want to get the value "-L474-TYYYGPvCChlZCS", how should I do?
推荐答案
要从DataSnapshot访问密钥,只需使用snapshot.key,您将拥有案例中该项目的相对密钥用户
for accessing the key from DataSnapshot just use snapshot.key and you will have the relative key of the item in your case Users
.then((onValue) {
onValue.map(data => {
var key = data.key
})
});
或仅使用Firebase文档中提供的内容
or just use as given in firebase docs inside then
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key; // you will get your key here
});
});
这篇关于从Firebase Datasnapshot获取JSON密钥值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!