'How to read data from Firebase Realtime database using Flutter/Dart

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rakesh Das