i am trying to get some data from firebase and than use that data to take other information. I am trying to do it using a future function. The problem is that everything works fine but the list comes out empty at the end and i dont know how to make it wait until the values have been added to the list and than appear. As you can see in this photo, the list comes out empty and than after print out the results and when you look in the code its vice versa. I tried some stuff using the await but with no success. https://gyazo.com/4dc9b3b4d459270c7c4bf146baa6ae25 What do you guys think?
Future <DataSnapshot> getData()async{
List lists = [];
await databaseReference.once().then((DataSnapshot snapshot) {
Map < dynamic, dynamic > values = snapshot.value;
values.forEach((key, values)async {
await FirebaseDatabase.instance.reference().child("Users/" + values["UID"].toString() + "/Name").once().then((DataSnapshot snapshot) {
lists.add(snapshot.value);
print(snapshot.value);
}); }, );
}
);
print(lists);
}