Reputation: 21
I am using Flutter and I would like to retrieve some data from my realtime database Firebase. I have the following data stored in the my realtime database Firebase:
How can I get each piece of information from it? For example I would like to get the name 'Tom' only?
Upvotes: 0
Views: 2911
Reputation: 226
if your data in RTDB is Object
, you can use casting type from result object to Map?
then, convert it to collection .map()
. here example:
final resultDataRealtimeDatabase = item.value as Map?;
final Map<String, dynamic> userData = resultDataRealtimeDatabase.map((key, value) => MapEntry(key as String, value);
final userModel = UserModel.fromJson(userData);
Upvotes: 0
Reputation: 63
FirebaseDatabase.instance.ref("users").onValue.listen((event) {
final data =
Map<String, dynamic>.from(event.snapshot.value as Map,);
data.forEach((key, value) {
log("$value");
});
});
Upvotes: 0
Reputation: 29
Reading through firebase documentation you can see that how we read and write data from RTDMS firebase.
static Future<List<PostModel>> getPost() async {
Query postsSnapshot = await FirebaseDatabase.instance
.reference()
.child("posts")
.orderByKey();
print(postsSnapshot); // to debug and see if data is returned
List<PostModel> posts;
Map<dynamic, dynamic> values = postsSnapshot.data.value;
values.forEach((key, values) {
posts.add(values);
});
return posts;
}
your can call this function at the time of build the widget or after any action
Upvotes: 1