How do I get specific values from a datasnapshot in flutter?

ε祈祈猫儿з 提交于 2020-05-13 04:54:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!