How to set Firebase Custom DocID using Dart - firebase

I'm using Flutter/Dart in my app, I can register users with de next sentence
Firestore.instance.collection('users').add(toMap(user));
but Firebase, creates the document with an automatic id, I want to create the document with the username to perfom the queries based on the username. (Find the products using the username)
I tried set the id in the User DTO but Firebase save it with automatic id.

Use the user's id to create the document like this
Firestore.instance.collection('users').document(user.uid).setData(toMap(user));

Related

How to store additional data in Flutter and firebase Authentication method

Good Morning,
I have a simple query, I am using firebase Authentication as a sign-in/ signup method to my flutter app, what is the right method if I want to store additional data, such as name, age and etc...
If you look at the class User that is defined in User.dart for instance that ships with the Firebase SDK for Flutter, you'll see various properties of the User class, including but not limited to:
String? get displayName
String? get email
bool get emailVerified
bool get isAnonymous
UserMetadata get metadata
You might see the metadata property and think Aha! Maybe I can put my extra data there, but if you look at that class' properties and code you'll soon realize that it's not going to allow you to store additional properties in it either.
So the User in Firebase is not the right place to store additional information about that user itself! That's the take-away I want you to get from this answer.
The right way to go about doing this is to store your additional information per user inside your Firestore Database. Create a simple collection and name it something along the lines of UserInfo and in there per user-id, store the additional information that you need per user, and add a field to every object in that collection named user-id and store the user.id in that field. That way you can always do a look-up of user information per user.id.
As i understand your problem to store additional data of a user after login. For this you can use Firestore database and create collections for the fields like- name, age and etc...
https://pub.dev/packages/cloud_firestore
Have a look into this library.
https://medium.com/firebase-developers/cloud-firestore-basics-in-flutter-68c7ec42eeca
To understand firestore go through this article.

Limit the retrieve of documents from Firebase on Flutter

I would like to learn how can I limit the retrieved document from Firestore quantity for each user. Is there any possible way to achieve this with shared preferences? Because I do not use a login or registration protocol for the user. I am using shared preferences in order to recognize the user.
Single user should retrieve only 1 document each day. This is what I want to implement.
Also I would like to learn that how can I save and show fetching date of the document?
You can do the following in Firebase:
Query query = cities.orderBy("name").limit(3); // descending order
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2); // ascending order
You can take a look at the documentation, there are the corresponding methods.
https://firebase.google.com/docs/firestore/query-data/order-limit-data

Can I subscribe to multiple firebase documents based on an array?

My firebase database has a user profile collection, where all the documents are UIDs, each document has a field called subscriptions which is an array of strings, each string being other UIDs.
I have a second collection for host content, where each document contains fields such as imageurl and caption, essentially posts that user has made.
Is there any way I can use the streamprovider on the host content collection to only show the posts by the UID the current user has a subscription for?
There are no SQL-like joins in Firestore, so your only option here (without changing any data) is to add a new listener on each document in hostconent that matches what's in the profile. There is no way to do this with a single query.
If you want to do this with a single query, you should have a field for each document in hostcontent that identifies which profile it's associated with. Then you can make a single query using that field as a filter to find only the documents for a profile.

How to write to a document and read the id of it within a single transaction in Firestore?

I am doing the user authentication where I have this case:
Read from vendor_type document and if it returns null(doesn't exist) then continue the transaction,
Create new user using .auth().createUserWithEmailAndPassword(email,password),
Read the new users ID,
Write to vendor_type document some of the new user's detail such as name, surname, userId -->> userId is the problem, how can I create a user and get the ID within a single transaction, can I even do that? ,
Take the newly created ID of the user, and create a new vendor document with that ID.
So far I don't have any code to post because I don't know if this is even gonna work so I didn't start. If you have any idea how to implement this, please let me know. The main issue is getting the user ID while still in the transaction.
At the time of writing, it is not possible to combine in one transaction the creation of a user through the createUserWithEmailAndPassword() method from the Auth service AND a write to the Firestore service.
They are two different services offered by Firestore and therefore you cannot combined calls to these two different services in one transaction.

Dynamic Group Security in Firebase Storage

I've reviewed the documentation for Group Security for Firebase Storage where it suggests to use custom tokens or including group information in file metadata, however, when a user leaves a group, I don't ideally want to be updating the metadata in every file or having to create a new folder and update the group token information.
If this were the Realtime Database, I'd check to see if the user was still a group member. Is there a more elegant solution along these lines that I can explore?
Many thanks
yeah with custom tokens you can associate additional attributes with the user. The trick is when you create custom token for a given user id, you can add additional claims and use them to evaluate the access rules. In your case you can add the group id to which the user belongs as additional claim when the token is created on your server using which the user logs-in from the client app. So whenever the user leaves your group, his group id association is removed and hence when you recreate the token for that user, you will no more associate the group id in additional claims.
Sample Example
public static String createCustomToken(String userId, Map<String, Object> additionalClaims) {
FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount(new FileInputStream(FIREBASE_ACCESS_FILE)).build();
FirebaseApp.initializeApp(options);
Task<String> customToken = FirebaseAuth.getInstance().createCustomToken(userId, additionalClaims);
return customToken.getResult().toString();
}

Resources