how can one user modify a data in firestore which will not affect other user data in the app? - flutter

I am new to flutter(and coding),I don't know if I specified the question clearly but what I want is for when a user "Follows" or "subscribe" to a product it goes to a listview but it changing the data globally in firestore, so in every user's listview has the same data subscribed. I want make it personalized for that specific user only, other can change the data too which will only affect that specific user only,
think of it as ecommerce cart function where every user has different thing in the cart
I did make a collection for "product" but every user getting data from same collection and it changing globally.
how can I make common subscribed data personalized for each user which will not conflict with others data.

how can I make common subscribed data personalized for each user which will not conflict with others data.
Every Firebase user has an id, you can use the user's id and set/query documents based on that.
For example, to set the data:
.collection('bla').doc(USER_ID).set({"name": "John"})
and to retrieve the data based on the id:
final snapshot = await Firestore.instance
.collection('bla')
.doc(USER_ID)
.get();
See also
How to get the current user id from Firebase in Flutter

Related

check weather the user readed data or not in flutter using firestore

I was developing the chat app in flutter using firestore, the users successfully chat one-to-one. but I also want to send the notification to the user that someone sends him/her a message. For that I decided to use the cloud messaging service of firebase, but how can I check that the user read the message or not.
There is nothing built into Cloud Firestore to track which user has read a specific document already.
The two most common approaches that I know of are:
To track for each individual message and each individual user whether they have read it, so by keeping a list of the user IDs in each message document. While this potentially stores a lot of extra data, it is the most direct mapping of your use-case.
In most chat apps though, tracking of what messages each user has read is done as an "up to here". So if the user has read all messages up until a certain one or a certain time, the assumption is that they've also read all the messages before that one/moment.
For this model you can instead just keep a single document ID or timestamp for each user (i.e. readUpTo), instead of having to mark each message for each user that has read it. In your UI, you then determine whether to show each message as unread based on the message ID/timestamp compared to the timestamp of readUpTo.

Firestore chat private and group

I have this chat structure for groups and private in real time, is there any way to structure to get the images and names of the people who chat? currently using UserIds I do a separate query to get it, but if someone changes their name or image it won't update.
You typically would start a RTDB listener for the nodes of the RTDB that contain the information for each of the users. Alternatively, you could copy all of the relevant user information into "current chats" and if a user changes their profile information you could also copy that new information into any "current chat" sessions.
In my app, I maintain user profile information in a profile collection in Firestore, and maintain the chat details in RTDB. So I have a realtime listener for the chat (RTDB) and a realtime listener for firestore.collection('profile').where('uid', 'in', arrayOfUserIDsInChat).
(Currently the way I have things, I am limited to the number of user profiles I can listen to ... in is limited to 10 ... but that is sufficient for my current requirements)

What is the correct process of keep consistency of data at add to cart functionality when using flutter_bloc?

I am implementing add to cart functionality in a flutter app with using BLoC pattern as state management, only a user who is logged can add products to the cart and as the first step, I want to store that cart data locally before trigger a POST request to the database. Also, there should be a facility to user to remove items from cart if it's unnecessary. Further, I want to keep the cart in the same state after a user re-open the application who logged in before. Is it possible with flutter_secure_storage and how I do in a proper way? And, how I maintain a cart to each user?
You can store the items of the cart in some persistent database or storage, so when the user opens the app again the items would be pre-loaded in the cart bloc. For this purpose, you can use the Hive, Shared Preference or sqflite. Use which one works best for you.
The proper way would be to store items in storage right after the user adds them, and clear the storage right after the user clears its cart, so he does not see any previous item upon opening the app next time.
Maintaining the cart for each user in persistent storage should not be a problem as it is stored in the mobile's memory. But if you are asking for multiple users using the same device having different cart items then you can assign a unique id or value to identify which user has logged in.

Persisting cart Items on my e-commerce website

I'm planning on an e-commerce website and would like some help!
The functionality I want is that whenever a user logs in, I will use the user's email address to fetch from my MongoDB database to retrieve all of the user's cart information (what the user has put in his/her cart from last time). However, I worry that if the user accidentally closes their tab, or closes their window, and visits the website again, then I would need to fetch from my database again to get the cart items. My question is that would it be better to store the cart item on a local storage or session storage. And then, once the user goes to the checkout page, I then store all of the cart items back to database. In that way, I don't need to store to and retrieve from the database everytime the user adds an item to the cart.
I'm using React/MongoDB. This is my first time building a e-commerce website and I would like to know how other people usually handle this problem, and whether local storage, session storage, or there are other ways to handle this!

Implementing visitor counter in flutter for web

I'm trying to implement a visitor-counter on a website built entirely with flutter-web and I'm trying to accomplish this with nothing but plain dart code and some packages from pub.dev.
But the problem which has been bugging me is that I need to find a way to uniquely identify users based on their browsers or their devices so that I don't end up incrementing the counter for the same person again and again upon a revisit.
So far what I've thought is that I could use firestore for keeping track of the total number of visitors and display the same on the webpage upon startup and use some caching package like dcache or localstorage (like described here) to keep track of users who are re-visiting the same webpage.
Is there any better approach to this problem? Any help would be appreciated 😁
(ps: I have no prior web dev experience)
Since you talk about users, I immediately think of using Firebase Authentication.
There are two ways you could use that here:
With anonymous authentication, you could create a unique ID for each user without requiring them to enter credentials.
If you want to be able to identify the same user on different browsers/devices, they will have to sign in with one of the supported providers (email/password, phone number, email link, facebook, google, etc).
No matter which one you choose, each user will now have a unique ID (called UID in Firebase terms), which you can use to identify them in the database. For example, you could create a document for each user in the database with their UID as the ID of that document. That way you're guaranteed that each user will have no more than a single document.
You'll still need to count the documents, for which I recommend checking out the distributed counter extension.
Also consider if you want to use the Firebase Realtime Database instead of Firestore here, as it might give you a simpler pricing model, and has a built-in presence system. You'd still use Firebase Authentication as before, but just swap the database.
There is a short way instead if you just want to capture number of visitors on your webapp.
I created a collection called "Visits" in firestore and generated auto id inside which I added a field called "count". That's it!
Now you just have to call this method in your first screen's init method :
updateCounter() async {
final cRef = FirebaseFirestore.instance.collection('VISITS');
await cRef
.doc("mS9fCXjvkpREt8LvanSb") //Paste the id which was generated automatically
.set({"count": FieldValue.increment(1)}, SetOptions(merge: true));
}