How do I get specific values from a datasnapshot in flutter?
18,857
Solution 1
here is what i did
Map<dynamic, dynamic> map = snapshot.data.snapshot.value;
map.values.toList()[index]["pic"]
Solution 2
if you are sure snapshot have child's use value member as Map Object
snapshot.value['key'];
Solution 3
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
Related videos on Youtube
Author by
Daniel
Updated on September 15, 2022Comments
-
Daniel over 1 year
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; }