Retrive node data from firebase database - firebase

I have linked my app with a firebase database and i am wanting to retrieve the string of one node from it.
The node I am wanting to retrieve is shown below with the name of 'timeStamp'. Is there a way i can retrieve this text and then print it?

The answer is covered in the Firebase documentation guide
Reading Data
and here's an example:
let ref = FIRDatabase.database().reference()
.child("Users+infomation/ff..etc/timeStamp")
ref?.observeSingleEvent(of: .value, with: { snapshot in
let val = snapshot?.value
print(val!)
})
*this is Swift 3

Related

How to get data from firebase sub collection?

Im trying to get data from firebase but im a bit struggling . I have this videos collection where I saving video ids and thenevery video has documetnfield and also a sub collection called user votes . In side that im saving the user votes from the ratingbarindicator
this is how to collection looks
So what I want is every document of the user votes sub colletion and then the each rating field .
but how can I do that ?What I want is calculating that together Hope anyone can help
To read the data from all (sub)collections with a given name, you can use a collection group query.
FirebaseFirestore.instance
.collectionGroup('uservotes')
.get()
...
Also see:
Is wildcard possible in Flutter Firestore query?
Fetch all the posts of all the users from Cloud Firestore
you can go through collection and document like this with firebase:
final querySnapshot = await FirebaseFiresotre.instance.collection('videos').doc([theVideoDocumentID])
.collection('uservotes').get();
final docs = querySnapshot.docs;
for(final doc in docs) {
final data = doc.data();
//handle each document here
}

Fetching data from sqflite and sending it to firebase

I made a local database using sqflite to save data locally and i need to send it to a firebase in need
so far the function i used to get the data is:
Future<List> getNameAndPrice() async{
Database db = await instance.database;
var result = await db.rawQuery('SELECT $columnName , $columnAmount FROM $table');
return result.toList();
}
the other function i use in the other page of flutter to get the data is:
Future sendtofirebase() async {
var orders = await dbHelper.getNameAndPrice();
print(orders);
}
so far the print is just to check the data i get and i get the data in this format:
I/flutter (21542): [{name: Regular burger, amount: 2}, {name: Cheese
burger, amount: 1}]
i just want to find a way to like fetch this data from the variable and then send it to the firebase database i've made firestore collectionreference and everything i just don't know how can i get the name alone and send it then send the amount of the item etc.. for each order (how to get each item individually and send it to the firebase db).
really looking forward for answers cause i've spent time looking and i am stuck there..
ps. i am still a beginner and i even don't know if i should use both sqflite and firebase
other than just saving everything in the firebase itself.. thank you for reading.

Firestore Collection (with Subcollection) to Sqflite Item in Flutter

My application is using firestore database. I design categories and its subcategories of the content by using subcollection.
To make offline mode feature;
I want to save this firestore data to sqflite database.
I learned that I convert firebase data map to text for sqlite
But I counl't find a way to locate the subcollections.
var catagorySnapshots =
await _firestore.collection("categories").getDocuments();
List _list = [];
catagorySnapshots.documents.forEach((_snapshot) {
CategoryInformation categoryInformation = CategoryInformation();
CategoryItem curCategory =
CategoryItem.fromCollection(_snapshot.documentID, _snapshot.data);
categoryInformation.categoryList.add(curCategory);
print(curCategory.title);
//
// sub collections
//
});
I know its too late to answer, this might help someone else.
You have to try getting the information from snapshot.data['field'] like so and covert it to text as well.
Let me know of that works.

How to retrieve data from firebase and then update the data using Swift5

After the current user watches the video ad, it will retrieve their point (which is stored in the firebase) and add 1 to their point. Then, it will update the document and also display the number of points the user has. It seems like everything is okay, except, I can't do scoreText.text = point
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
let dBRef = Database.database().reference()
dBRef.child("Users").child(Auth.auth().currentUser!.uid).queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
guard let dict = snapshot.value as? [String:Any] else {
print("Error")
return
}
var point = dict["point"] as? Int
point!+=1
dBRef.child("users").child(Auth.auth().currentUser!.uid).setValue(["point": point])
scoreText.text = point
})
How would I display the user's points?
[UPDATED CODE]
Changed code to access Cloud Firestore, not Realtime Database
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
//[START update_document-increment
let docRef = db.collection("users").document(UserID!)
// Atomically increment the population of the city by 1.
docRef.updateData([
"points": FieldValue.increment(Int64(1))
])
//[END update_document-increment]
//insert point value here
scoreText.text =
The code you're showing accesses the Realtime Database, while he screenshot shows Cloud Firestore. While both databases are part of Firebase, they're completely separate, and the API for one doesn't apply to the other.
To fix the problem, you will have to either use the Cloud Firestore API, or enter the data into the Realtime Database.

How to get user specific data in Firebase Swift 3

I want to retrieve data of current user in the application. How can I do that. ?
Here is my current user's firebase id :
And my table node is :
Here it is clear that my uid and nodes title are different. So how can I get particular user's data.
My code till now:
self.ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
self.ref.child("member").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists() {
// handle data not found
return
}
})
You are getting different IDs, because the first one Wnlxl... is the userID and the other -KVvR664... is an autoID.
You have set .childByAutoID following the member, if you want to retrieve the data of the current user then, you have to put userID following member.
Then you can easily retrieve the user's data by userID because each and every detail will be saved under userID.
Hope this code will work for you.
let ref : FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.child("member").child(user!.uid).setValue(["name": self.fullName.text!, "email": self.email.text!, "mobile": self.mobile.text!, "doj": self.doj.text!])
Your Firebase screenshot is incorrect I think - your code will work fine, but how you create these members are wrong.
When creating the members you are saying .childByAutoID which you should be using Auth.auth().currentUser?.uid. If that makes sense. Let me know.
Edit: to create a user with userUID data in the node member do the following:
if let userUID = Auth().auth.currentUser?.uid{
ref.child("members/\(userUID)").setValue(*YOURMEMBERINFODICTIONARY*)
}

Resources