Read data from Firebase created by loggedin user - Flutter [closed] - firebase

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've seen a lot of examples online on how to write and read data to Firebase. However, I want to know how to write and read data from Firebase only created by logged in User.
How do we bind such data to a user?
Thanks

It might depend on how your data are oranized. Read through this:
https://firebase.google.com/docs/firestore/manage-data/structure-data
A simple example:
// snippet for pulling from data once it is there
await FirebaseFirestore.instance
.collection("USERDATA")
.doc(userID)
.collection('DOCUMENTS')
.get()
// one way you might supply the function that puts data up to firestore.
uploadToCloudStorage(
userID: user.fAuthUser.uid, fileToUpload: File(filePath));

Use the userId to as the docId for the documents in firebase. Here is an example.
createOrUpdateUserData(Map<String, dynamic> userDataMap) async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
DocumentReference ref =
Firestore.instance.collection('user').document(user.uid);
return ref.setData(userDataMap, merge: true);}

Related

Firestore 'arrayContains' does not work in flutter app [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to fetch the data from firestore. So, I created function to get data as follows. It works with 'isEqualTo' but does not fetch when I change the where clause to 'arrayContains'.
Query<Map<String, dynamic>> detailCollection =
_firestore.collectionGroup(collectionName);
return detailCollection
.where('title', arrayContains: 'cb1')
.snapshots();
my fiestore rule is as follows,
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
I tried to get the data from 'posts' collection.
Appreciate if anyone can show me, what is the cause of the issue?
What you're trying to do will not work. array-contains queries only work with fields that are actually arrays. Your cb1 field is just a string. It's not an array, so it would never match an array-contains filter.
If you want to use array-contains, then you will need to use an array type field. You might want to review the documentation, which pretty clearly states that the field must be an array.

Flutter - Render widgets based on Cloud Firestore data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a flutter ecommerce grocery app and I have some trouble in backend.
I have a products screen that has addtoCart function that adds that item to Cloud firestore as follows
Future<void> _addToCart(final uid,
String title,
String price,
int unit,
int total,) {
CollectionReference cart = FirebaseFirestore.instance.collection(
'users/$uid/cart');
return cart
.doc(title)
.set({
'title': title,
'price': price,
'unit': unit,
'total': total,
'order_status': 'in_cart',
'order_placed_time': DateTime.now(),
});
}
This places products that have been added to cart as follows
cloud firestore image
Now the problem I am having it to take these products that I have added to firestore and render them in my carts screen which is subject to changes as user constantly adds them. How can I render them should I used SharedPrefs?
A quick solution would be the usage of StreamBuilder<T> which is able to listen for changes on Firestore and update your UI in in realtime.
// Define this somewhere
final firestoreStream = Firestore
.instance
.collection('abc')
.snapshots();
// Then in the 'build' method
StreamBuilder<QuerySnapshot>(
stream: firestoreStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return const SomeWidget();
}
if (snapshot.hasError) {
return const ErrorWidget();
}
return const Center(
child: CircularProgressIndicator(),
);
},
);
While this is quick to implement, you'd better use a state management solution such as Bloc or Riverpod (or anything else).
In particular, the stream would be handled by the state management library internally which will rebuild the UI accordingly. In this way, you can also avoid undesired rebuilds, implement caching, filtering and much more.
Please note that I am not talking about performance because StreamBuilder<T> is very good. Instead, I'm saying that a state management library should take care of the stream for a better separation of concerns (which keeps the code easier to maintain).
I would recommend looking into using a StreamBuilder example this basically allows your app to listen to a collection in firebase and rebuild widgets whenever there is a change. This way you will never have to manually trigger a rebuild whenever a document in Firestore is created, updated, or deleted.

Is it possible for Firebase to alert me based on a certain event parameter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an event from my Android app on Firebase that if it reaches a certain value I would like to be notified. Is this possible with Firebase?
Basically the event sends a string with what happened on a certain service request. If the service request failed it sends a certain string. I want to be alerted when that certain string is more than 10% of all events. How can I do that?
Thanks.
You can have a cloud function that is listing for events into the collection/doc you are interested in... when you get the error, you write to this collection lets say it is called WHATEVER_COLLECTION_YOUR_ARE_GOING_TO_LISTING_TO
It would look something like (i dont know your specific case, this is only to get you started):
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
const firestore = admin.firestore()
const counter = firestore.document(`WHATEVER_COLLECTION_YOUR_ARE_GOING_TO_LISTING_TO/{doc}`).onUpdate(async (change, _context) => {
const newData = change.after
const data = newData.data()
if (data.MYSTRING === 'SOMETHING HAPPENED IT IS MORE THAN 10%') {
// USE SENDGRID OR TWILIO OR WHATEVER TO NOTIFY ME
}
return Promise.resolve(true)
})

Meteor How to Avoid Publishing All [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I thought I understood pub/sub in Meteor until I ran into this issue.
Suppose you have many blog entries that are meant for public consumption and a user navigates to /:blogId.
You use something like
Blogs.findOne(FlowRouter.getParam('blogId'));
Currently, on the server side, I'm publishing all blog entries.
Meteor.publish("blogs", function () {
return Blogs.find({});
});
I'm guessing I should only publish the blog entries that are requested with something like:
Meteor.publish("blogs", function (_id) {
return Blogs.find(_id);
});
What is best practice here?
You have it exactly right in your question:
Meteor.publish("oneBlog", function (_id) {
return Blogs.find(_id); // must return a *cursor* or array of cursors, not an object
});
Meteor.publish("allBlogs", function () {
return Blogs.find();
});
From the client subscribe to the oneBlog based on the route parameter:
Meteor.subscribe("oneBlog", FlowRouter.getParam('blogId'));
You can make another publication (which returns only one) for specific route.

How to parse xml and simultaneously save to sqlite asynchronously in iOS 7? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am parsing XML Document by using TBXML but I have to Parse XML and store data to SQLite asynchronously with notifications [that is Parsing and storing data in SQLite]. Please help me to overcome this problems. Thanks in advance....
For that You can use NSNotificationCenter and GCD,
First set NSNotificationCenter for your process using,
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(dataStore)
name:#"dataStoreComplete" object:nil];
- (void)dataStore
{
NSLog(#"Received Notification - Data stored in databse");
}
You GCD for parsing and storing in database
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// switch to a background thread and perform your expensive operation
 // parse and store all data in sqlite,
dispatch_async(dispatch_get_main_queue(), ^{
// switch back to the main thread to update your UI
 [[NSNotificationCenter defaultCenter] postNotificationName:#"dataStoreComplete" object:nil];
});
});

Resources