Overview:
Firebase is one the most popular database which is also used by Flutter App Developer. In this guide I'll guide you how to Fetch one single node data in Flutter. This guide is very useful for you if you are working with Firebase database and you are trying to fetch one single node data from Firebase Database.
Setup Firebase Package in Flutter projet:
If you want to fetch data from Firebase first of all install the Firebase Database package in your Flutter project. So for doing this go to official pub.dev site and get Firebase Database and Firebase Core package's. I'm also sharing these packages here but i will recommend you to use Latest updated version of these packages. Add these two packages in your Flutter Project. These two packages are provided by Google Check details here
firebase_database: ^10.2.1
firebase_core: ^2.13.0
Code in Flutter to Fetch Single Data from Firebase:
After installing these packages just use this given code in your Flutter App to fetch single node data. By the way there are many other different ways are available to fetch the single data from Firebase Database. But as per my research this one is the very easiest method to fetch data from Firebase.
final _DBref = FirebaseDatabase.instance.ref("Name");
String age;
_DBref.child("age")
.onValue
.listen((event) {
setState(() {
age= int.parse(event.snapshot.value.toString());
});
});
Explanation of Code:
This is the explanation of above code that i share with you to fetch data from single node of firebase. So now i will explain how this code will work and how to setup this code and how this code is working.
Instance Of Database:
First of all create the instance of Database according to above code i create the Database Instance _DBref after that i write the database name let's suppose Name is my Parent node and inside this node i've multiple child nodes like age node, class node, email node etc.
So First of all i give the refernce of Parent node when i am creating instance of Database Reference.
Fetching from Specific Node:
In this step i use the _DBref instance of parent node and fetch the other nodes data. I already add the parent node name in _DBref so here i will just call the child nodes of parent node. So let's see the above of first i write the _DBref and than i use child keyword which means i am targeting the child of my Parent Node. So in the child add your child Name (If you have another child node after this than just use the .child() once again to target the next child). After this use .onvalue (Because we are fetching value) than use .listen keyword.
Showing the Firebase fetched data in Flutter App UI:
Inside .listen({....}). save the fetched value in the varialbe with the help of event in the above example i am using event.snapshot in order to get exact value. Save the value in variable and call the setstate method setstate will refresh your UI and this fetched data will start showing in your UI where you call this.
In my case i Fetch age from Firebase database so i create a text widget and call the Age Variable so when UI will refreshed than age start showing age of the user.
Where we write this Firebase Fetch code:
Create a new method and write this code inside it and than call this method inside InitState() once screen will opened InitState() will use this method to fetch the data and show it on your UI.
Major Purposes to fetch single node data in Flutter:
We mostly use this code in our Flutter Applications when we need to show one single data to users. Like Some specific information of user so we use this code. Let's suppose i have an application and in this application i have a profile section for user where user will see its specific information like Email id, Name, Age, Country etc. In these type of cases we use this Flutter Firebase Code to show the specific information. But it totally depends on Developers how he use this code and what thing he is trying to fetch from Firebase.
Conclusion:
As you can see how its easy to fetch specific data from Firebase. You can use code in your Application to fetch user's data or for any other thing it totally upto you but this one is the easiest way in flutter app development to fetch one single node data from Firebase.




0 Comments