0

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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0
FirebaseDatabase.instance.ref("users").onValue.listen((event) {

 final data = 
 Map<String, dynamic>.from(event.snapshot.value as Map,);

 data.forEach((key, value) {
     log("$value");
 });
});

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.