I am trying to make a firebase database call to retrieve all row of one particular column in the firebase database of my app and display it in a textbox in my flutter app.
That is, to retrieve all phone numbers in the Users table.
For instance, in SQL we can do like this:
SELECT (PhoneNumers) FROM Users
I am new in flutter/dart, I need some help.
Its really easy.
First of all, setup and initialize firebase in you project. Then make a function and create an instance of firestore like this
FirebaseFirestore firestore = FirebaseFirestore.instance;
Then make something called Collection reference:
CollectionReference users = await firestore.collection("users").get().then((querySnapshot) {
querySnapshot.docs.forEach((doc) {
print(doc["PhoneNumber"].toString())
});
This will grab every collection from Users, loop over them and then print out the document PhoneNumber to console. There is no way to just get a single document field from firestore. It might seem weird, but its quite useful at times. Right now, you have access to every document field of every user of your app.
Related
So my problem is that I know how to write in Firebase, update it, and read data... but now I need to get a specific field from a document and save it in some variable or string, and I am stuck there.
Picture of what I need
Here is a field I need to get.
Here is where I need to save it
So I need it to be saved so I can use it in code, specifically as an isEqual value. Because my goal is to display Players that play in that club. And every game its another club.
I found solution:
String? valueFromFirebase;
Future<String?> getData() async{
var a = await clubData.doc('Home').get();
setState(() {
valueFromFirebase= a['homeClubName'];
});
}
So when I need value from field ( in my case homeClubName) I just call string valueFromFirebase where I need it.
firstly you have link you app with firebase then you need to import 3 plugin firebase core plugin firebase Firestore and cloud firebase then you need to in your Main.dart here first you create a Class Then you control it by TextController then you'll post you data After the data will successfully post on firebase then you will get response from firebase.. Actually I'll work on Firebase you can say rule Put Get Update Delete & fetch in other word these are the Queries.
I am using Drift for the first time and I have been able to create the tables in the database and insert into a table in the database. Now I want to show a field value from the table on my app, for example, a table called Employee that has the employee name in the "name" field, I want to be able to display the employee's name at the top right of the app when they log in. So I want to get the value of the name field for that employee.
How can I achieve this?
A lot of examples I see are for displaying Lists and that's not really what I'm looking for.
I tried to do a get on the table but I don't know how to get the snapshot of the table's data.
Future<List<Employee>> getEmployee() async {
return await select(employee).get();
}
Please how can I achieve this?
To get a value from Future you must use await
for more details pls have alook in the official documents
So You can get employees list like this
void getEmployees() async {
final employeesList = await select(employee).get();
}
Where employee is the table name.
Due to a weird situation where my Flutter app crashes if a query for authenticated user returns null, I'd like to do a short-term workaround by creating a transaction automatically whenever a user is created in the user collection.
This should reference a record in the Transactions collection by the Document Reference.
However, I can't seem to create a custom function which simply returns that value: /ads/WgtrMUIwjmVeyjQ6UwMl
I'm working on something like this:
I want to pass it through a Function as return value.
Thanks.
I have a flutter app that uses a real time database through Firebase. I know what I want to do but am so new to this that I am unsure of myself.
When a person scans the barcode, it shows up as a button. When they search for that item, it returns the proper attributes listed in the database. Great.
But what happens when the item isn't in the database? I want to create an if statement that is something like this in pseudocode:
Search for UPC:
If search results in null run "add barcode" method.
Create text inputs for all data required to add to DB.
If search results in hit, display data.
Run method that displays data
I've used the Real-Time Database with this setup:
->users
->uid
->name
->email
->other info
If I wanted to save the user data I would use my User class and then set the object in the database like this:
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
FirebaseDatabase.instance.reference().child("kullanıcılar").child(user.uid).child("takip").set({"uid":"uid"});
I tried it and it works fine.
But how can I retrieve these values from the database? For instance, once I set a User object as shown above, I may want to retrieve the user's email. I don't mean getting the email through the sign-in provider. I want the email through the real-time database. I've tried working this out for a while now but the documentation isn't helping much. All it shows is to setup listeners in order to wait for changes to the data. But I'm not waiting for changes, I don't want a listener. I want to directly get the values in the database by using the keys in the JSON tree. Is this possible via the real-time database? If so, how can it be done because either the documentation doesn't explain it or I'm just not understanding it.Thanks.
You can retrieve the value once using .once(). Here's an example: https://github.com/flutter/plugins/blob/master/packages/firebase_database/example/lib/main.dart#L62
or better, with async/await:
Future<String> getEmail() async {
String result = (await FirebaseDatabase.instance.reference().child("path/to/user/record/email").once()).value;
print(result);
return result;
}
You can't retrieve the single value from firebase, it always return the DocumentSnapshot of the whole record. You can get a single record from user table by using user id like this:
getData() async {
final FirebaseUser user = await _firebaseAuth.currentUser();
return await FirebaseDatabase.instance.reference().child('user').equalTo(user.uid);
}
Get it like this:
getData().then((val){
print(val.email);
});