Add subcollection inside collection in flutter firestore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends now in this video tutorial we will see how we can add a sub collection into our uh collections like this one here you can see we are having a collection name usa and then we are having a document inside document we are having a usa here you can see that we are having all of the fields and then we are having three sub collections inside my document so you can see that here is the sub collection name you can see that we are having three different types of sub collections so in this video tutorial we will see how we can add this thing into our fire store like collection and we will insert sub collections over here as well so you can see that we are having a blank page over there so we don't have anything so let's start building some of the so let's inside our body let's add column widget so we have to just use cross access alignment cross success random dot center and we will use main axis alignment dot center okay so what this will does so let's add this in a safe area widget so our text will not go beyond the the so if we will just restart this so what it will do you can see we are having now white background screen okay so uh let's add text button so with on press of that button we will just add of our logic what should we need to do so let's give child sorry give childhood to a text widget so write down add collection okay simple only add collection so we will use the word sorry we will use the const keyword here so you can see the warning is gone so uh here is the i have created this method uh basically this method is inside my api service class this is the add collection method so let's call this method let's create a object for this let's say api service api service service is equal to api services so what should what we have done we have just created an object of our class so uh if we so we wanted to call this method from our class add collection we will just use uh here you can see add collection method so uh so this is so far we have just used this so okay first of all let's refresh the app so we can see that either so you can see that we are having button over there but you can't see uh because we have used both cross axis alignment and main axis alignment to center but we can see that we are not placing our button at the right part so let's give this inside the center widget so you can see that we are just placing our button inside uh center widget so uh if we go over there so we have to just create a so let's create a value called it so just remove these thing what we have to do just have to await and we called it as users because we have created a so let's create a new uh collection over there so called it as ftm so let's create users dot add method so what it will does it will just accept the the the object of map so we will just pass to let's say parameters let's say id we called it as let's provide id into like this and let's call it as sorry let's call name as your collection first okay so these are the two parameters so we can obviously use uh we can just pass these two parameter under there so while calling this object this method we will just pass the two parameters and and place this parameter here so we can just add these two parameters so uh so what should we need to do here first we have to use a variable let's say called it as a result and we have to store the variable inside this because we wanted to get the id of that result let's say we wanted to add one item and we want to get back the added id here so if we will just use result dot so you can see we are getting the id see okay so let's forget about this and what it will does okay so let's refresh it it's a very simple tutorial if we click on add collection button so what it does if we just refresh our database so here you can see that we are getting a collection over there and and this is the id that we just wanted to uh get because we wanted to insert a sub collection for this for this uh document so we will get this id from our uh you know from our uh where we have done okay so here you can see this is the id we will get from this so this is the id that we will getting after calling this method okay so what we will do we wanted to just add a sub collection over there okay so uh i have created a simple method for you guys so it's very easy for you to just add a sub collection over there so i will just write it down for you so you know let's call it as future basically this is just my logic which you can just write your own so let's add add multiple collections so uh we have to written string over there from there called it as success but you can see that we will not the error is not gone away so you have to use the async keyword over there so what this will does so we have to just call this method and what it will does it will just take the let's call it as string id so so let's add this method so now we are reference referencing to ftm and we wanted to add another uh you know another sub collection add multiple collections okay so what should we need to do sorry okay so now uh you just have to use method called sorry uses dot okay so first of all you wanted to get a document so uh that you have created so far so you can see if we just go over there and write users.doc so we have to provide the id in which we are just wanted to add uh our collection so here you can see we are just just call the collection keyword over there collection method over there and we have to write our collection name so let's write it as uh spending history okay so now you will just add you will just add a map of object so let's copy this map and place this map and let's call it as id and we will get this id so we will just store that id we called it as let's say created at datetime.now okay so uh so now what we will do we will just have to call this method call this method add uh multiple collections this is the method that we just wanted to write and what we will need to do we just have to pass the id over there so id would be this result id so we have to use a weight keyword here so it will await it till uh okay so so what i am just doing here if the result is stored successfully so it will just added uh it will just get the id from this and we wanted to insert an other like another sub collection that we have just created a document so we wanted to create another sub collection inside that document so we will just get the id back from the from that collection and we will pass it inside here so what if we just refresh it okay so it is just refreshed and okay so if we will just create another document so you can see here the document is created and you can see that we are having us another sub collection inside our new document that is created so you can see as you can see that we are getting the id that is just like this and also the and also the the created ad that we have just used so far so i think this would be helpful for you this is a very simple task for me so if you wanted to add some of the other parameters so you can just pass inside this function like username password and email so you can just pass it and use these these methods these these parameters inside here and fast at parameters by as per your need so thank you from my side hope you enjoyed it
Info
Channel: muhammad adnan ijaz
Views: 12,524
Rating: undefined out of 5
Keywords: Flutter firestore subcollection, add firestore collection, add subcollection in firestore, collection inside document flutter firestore, firestore collection, firestore document, add multiple collections inside collection flutter
Id: MaZIWZ3AKrE
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Fri Apr 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.