How to save specific field value from Firestore to Flutter - flutter

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.

Related

Drift Flutter - Get Specific Field Value From Table

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.

How can I hard code and return a DocumentReference in Flutter using Firebase?

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.

How can I retrieve all row of one column in firebase flutter

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.

How do I read data from the Flutter Firebase Real-Time Database?

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

How to set more than one value to a child in Firebase using Swift?

I am trying to make a money related app which shows the user their spendings as a project to get used to Firebase. So I stumbled upon this issue; I can't seem to figure out how to add more than one expense assigned to a user. Whenever I add a new expense, the existing value in Firebase gets reset to the new value but I want it to store both the new and the old value. How can I do this?
There's something called "autoID" if that helps. I'll link that in a few moments.
Edit: It's used here.
childByAutoId() is what you want for swift. See Updating Or Deleting specific data section in the Read and Write Data on iOS
let thisUserRef = ref.child("users").child(users uid)
let expenseRef = thisUserRef.child("expenses").childByAutoId()
let dict = ["expense_type": "travel", "expense_amt": "1.99"]
expenseRef.setValue(dict)
will results in
users
uid_0
-Y88jn90skda //<- the node key created with childByAutoId
expense_type: "travel"
expense_amt: "1.99"
the childByAutoId is super powerful and allows 'random' node keys to be generated that contain your child data.
See the answer to this question and this other question for some tips on data modeling and queries.