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);
});
Related
I tried to use the new system in my flutter application using the supabase_flutter: ^1.4.0 package
It works but I have no response about the insertion
final response = await supabase
.from('order')
.insert(tmp)
;
the response is always null.
For example how can I get the progressive id of my insertion in table order?
I feel so dumb...
Simply adding the
.select()
clause we will get the resoult from insert
So the complete code look like this
final List<Map<String, dynamic>> response = await supabase
.from('order')
.insert(tmp)
.select();
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.
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.
I am working on a multiplayer react native game for android, it's just a text game.
For now, I've been working only on the client side but I'm slowly getting to the point where I need to choose a way to manage data. Since I'm always trying to learn something new - first time using RN too -, I came across Firestore and I'm curious if it's a viable choice from my project.
For example, in this game you can buy/open companies. If I were to handle this action in nodejs + MongoDB I'd send a JWT token to server/api/newcompany.
Assuming I'd have 2 MongoDB models, Users with all the user info and Company a list of all Companies in game. First I'd check if the User had enough money to open the company and then return some message based on that, my code would be something like this:
// routes.js
...
router.post('/api/newcompany',AuthCheck, Company.new)
// controllers/company.js
...
new: async function (req, res, next) {
if (!req.body.companyType || !validCompanyType) {
return res.status(200).send('invalid/missing message and other failed checks')
}
//assuming token data would be passed from auth middleware and would have company prices somewhere
const user = await Users.find({id:token.userId})
if (user.money < companyPrice) {
res.status(200).send('no money')
} else {
const newCompany = new Company()
newCompany.name = req.body.name
...
new.Companny.save(function(){
res.status(200).send('all good')
})
}
}
Looking at Firestore I'd have the same 2 documents as above. Users with a list of sub-documents(the users) identified by their IDs and a document Companies.
Would I be able to do the same users checks as above?
How would I handle that? Would I use the Firestore Rules to check the user has enough money or would I use my server with admin SDK as an intermediary to handle more complex ops and use the direct Firestore connection just for retrieving data?
Thanks for your time.