问题
I have this datasnapshot
"{post1: {pic: https://i.redd.it/ni6zhxh874011.jpg, title: title, desc: desc}, post2: {pic: https://i.redd.it/krj9miojg5011.jpg, title: awsdas, desc: desc2}}"
and I would like to retrieve the "pic" values from each post. snapshot.value["pic"] does not work it returns null.
thanks in advance
this is how I recieved the datasnapshot for my future builder
Future<Object> _obj ()async {
Object _objdatabase;
await FirebaseDatabase.instance.reference().child("Communities").once().then((DataSnapshot snapshot) {
print(_objdatabase.toString());
_objdatabase = snapshot.value;
});
return _objdatabase;
}
回答1:
here is what i did
Map<dynamic, dynamic> map = snapshot.data.snapshot.value;
map.values.toList()[index]["pic"]
回答2:
What you want is to iterated over values in your snapshot.value and add them to a list.
pseudo-code:
for (var value in snapshot.value.values){
myList.add(value);
}
Then you will be able to do something like this depending on your use case:
myList.forEach((v)=>print(v["pic"].toString)); //just an example
回答3:
if you are sure snapshot have child's use value member as Map Object
snapshot.value['key'];
回答4:
This code retrieve the key of node:
DataSnapshot usuario = await messagesRef.limitToLast(10).orderByChild("email").equalTo(mail_emparejar).once()
.then((DataSnapshot snapshot) {
print(snapshot.value);
user_uid=snapshot.value.entries.elementAt(0).key;
........
result=6TLD6RFIa0T0a3CuRW9FpB4HlXf2
来源:https://stackoverflow.com/questions/50554737/how-do-i-get-specific-values-from-a-datasnapshot-in-flutter