How to store data in Firestore Flutter - Cloud Firestore 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we are going to learn how to store data in firestore so first let's quickly have a recap what we are going to achieve today so our application is running let's click on sign up to register a new user let's add the details once the data has been entered click on sign up to register a new user and you can also see over here that we have a collection of users and inside this collection we have list of documents which you will learn in few minutes so we want to add another entry over here and inside the authentication portion another authentication will be added so let's click on sign up let's wait for few minutes and at the bottom you can see we have a success message and also the data has been entered and over here inside the mobile you can see the message has arrived automatically it's going to convert which is a previous tutorial for the Firebase authentication so this is what we are going to achieve today you can get the complete code of this tutorial and also about the previous tutorials from the coding with t and also link of all the videos upcoming and the previous will be inside the description below so before getting started don't forget to subscribe the channel and hit the Bell icon to get notified again here you can see we are creating flutter login application and the design portion is completed if you are new no need to be worried because this tutorial is not directly linked with the previous videos but if you are following the series you already know that we have completed the design portion for the login application and we also designed the dashboard profile section of the user and in the previous tutorials we already started our backend with the Firebase authentication has completed and from today's onward we are going to start Firebase five store Series in which we are going to learn how to create store update and delete the data and we will add the end store data inside our shared preferences cache today's reviewers to store the data so let's get started okay let's open the Firebase console and in the previous tutorial you can see for the authentication we cover the phone number and email authentication but for today you will go to this build and open the firestore database I open it here okay inside this file Cloud firestore database basically firestore database is not the first database introduced by the Firebase the first one is or the old one is the real-time database which only works in a Json structure but this is an improvised version of the real dream data which is called the firestore so released later on so both have their own usage and you can see over here is cloud firestore right for you you have to compare database between real time and Cloud firestore so firestore is basically going to store the data not in tables rows and columns if you're coming from the SQL background it's going to store the data inside collections then collections have multiple documents let's say we have a collection of users and then we have multiple user documents inside the user's collection right so you can consider this user as a table and then we have multiple values which we are calling as a document but there is a difference between both documents or not restricted to some specific string integers and specific columns we can add multiple values in one document and other type of values in other documents so there is no restriction like we have in the tables so let's first create database to learn it better start in a test mode click next choose your file store location click enable Okay so so this is the panel for our firestore you can see the first thing we need to do is to create a collection right the first thing that we are going to be pointing to is our database so we will point to collector files to dot instance so instance is this one so this is the instance where we are pointing towards our database then inside this instance we can access everything like in the collection so first let's create a collection let's name it uses click next and then as a document you can see we have to add something so let's say it's an auto generated ID we are going to store name of the user name is coding with t let's save it and you can see we have a collection of users inside we have multiple documents the first document contains only name and then we can create other documents with an auto ID so for the second argument you can say you've created a name and email let's save it and the second document contains email and name so in here you can see we have collection of users it's not restricted its documents to only contain name so the this document have two things and this one have one thing so again this is the two are strings but you can create date and time integers doubles anything you want and then furthermore inside a document let's say in this document I want to store address and then here you can see you created another collection of address inside the users this user has a value of name but it also contains another collection which is address and when we are going to open this you can see we have another document inside the collection which is which contains City and postal code right so this is how we can create collections documents and then again collection and document the sequence cannot be broken right okay now enough of the understanding let's dive into our replication okay so we are inside our photo login application and inside you can see we have added Cloud firestore to use the file store inside our application once added click on Pub get close this if you're following the series you know that inside the features we have authentication inside the authentication we want to create a model which is going to map the values of the user first one is ID full name email phone number and password and then this is a Constructor of that model the one more thing that we need to do is because we are going to slow the data into firestore so firestore doesn't and store data in this way so we have to map this data to Json approach so let's create another function name of the function is to Json and it's just going to return Json version of this model and you can see at the top I I didn't add ID because ID will be automatically added so we have full name email phone number and password so when we are going to pass the director to this model we will call Dot to Json and this data will be converted into this shape so once the model is created you can get the code from the description below once the model is created we first have to create a repository this is the authentication repository so let's create user repository so I have created a folder user repository inside a file user coaster dot this user repository is going to contain all the queries related to file store for the user like create update delete get all users and other things so I am using the getex controller so you just have to extend it with the getex controller and make it a static instance of our gatex controllers now to use the file store we first have to point our application to the database while adding the Firebase firestore instance if you see that it's not being added go to perspect.dml file and inside you have to make sure that Firebase core is minimum at 2.4.0 to use the latest Cloud firestore so go to terminal you just have to run this command which is photocop upgrade to iPhone's major hyphen versions you have to enter and it will automatically update your file Discord and Firebase auth packages to the latest and then you can run the flutter pop add float firestore command hit enter and it is going to add the latest dependency which is cloud file store like this click on pop get close the pub sector DML file go over here again and hover over the Firebase file store and you can see now we have a dependency which is a cloud firestore. import this and error will be gone so now currently we are pointing towards the root of our database so that's why I have named it DB underscore means it's private the first function that we want to create is to create user like this and this user is going to have values from the user model okay first we have to point to the database and then we will call collections because inside the database the first thing we have to go inside our collections so here inside the collections and we have to give that which collection we want to use it's a user's collection dot what we want to do we want to add something definitely it's a document we want a document of users and we will pass the user received at the top and it's going to give an error because we have to convert this user model to map of string and dynamic so map of string and dynamic means we have to convert the DOT to Json because when we are going to hover over it to this function this is the string and this is the dynamic value so this is what it's required over this add function you can see it requires map of string and dynamic as a data so we will pass we'll pass to this and if you want to see that it's completed or not when it's complete we can call the get dot snack bar Control Alt L over here when it's completed we are calling a function which is get dot snack bar this is a title which is success and your account has been created as a message snack position is at the bottom background color is this color text is this and also when we have an exception we can easily call Dot catch error and this is the function again we are going to call get dot snack bar title is an error something went wrong because we don't want to show an exception to the user so you'll only write something went wrong try again again this is snack position and that's it so you have catch error and we have dot when complete okay now you can see this is the only query which is performing and rest is only the ending of the data if it's Error if it's not so as we are performing Cloud function so we have to make sure that we call an await method because a weight is going to wait for the security to be completed otherwise it's going to execute this query and jump towards the next line so that's why we have to write a weight over here and make this function as an async function okay now you can see our user repository has been completed and the first step was to create a user model then we get I'm going to create a user repository in which we are going to store the data now we have to call this function from sign up screen so first let's go to sign up home widget and inside you can see we have all the text fields and inside the button previously first we learned email and password authentication then we were performing phone number authentication but for today's tutorial as a step number three we're going to use the same model we have created today and we will pass all the values we fetched from the text field to email password full name and phone number and you can see the ID is nullable so that's why we are not passing the ID over here and in here you can see we are calling the phone authentication to perform the phone authentication so we can either convert this function to save user's data and then we are going to perform the con authentication or we are going to use the same one so let me just duplicate it and we are going to change this name to create user and instead of the phone number we are going to pass user model as a user and instead of this authentication repository let's at the top create an instance of user repository by calling get dot put we are calling get output user repository using the user repository inside this method we can easily call user repository dot create user and we will pass the user inside it and that's it and after this we are going to call this function which is the phone authentication like this phone authentication we will pass user DOT phone number and also the second method will work and perform the rest of the things because this is an async function so we should have to call await method so first we want to show the data then we want to perform the font authentication provided by the user and you can see by adding an avoid function will be turned as an async function and return type will be Future Let's avoid so it will add a future with it so let's head back to sign up form and inside you can see we have to call instead of the phone authentication we have to call create user and we have to pass the complete user to it including phone number we are not going to OTP screen from here we will first store the user data once data is stored we will call authentication and redirect the screen to the OTV screen for the user to enter the OTP okay now let's try to run the application okay so our application is running let's go to the sign up add details once the details added click on sign up you have to wait for it and you can see the message your account has been created now we have to verify let's wait for the OTP to arrive and OTP has arrived and we redirected towards our dashboard screen this means that user account has created let's go to the profile and and this is the one we have just created with these details so this is how we store the data once the data is stored authentication has also been completed go to here refresh and you can see the phone number has been authenticated now each time let me just close this application rerun the application again and this time you can see it's not going to ask the user to enter login and sign up details instead we have directly redirected towards here and from here onwards we can easily get the data from getting these IDs we will learn in this in the next video so if you learn something new please like the video and you can get the complete code of this video down below in the description from the codingwitty.com once again thank you for watching take care Allah office
Info
Channel: Coding With T
Views: 72,290
Rating: undefined out of 5
Keywords: flutter firebase crud, flutter firestore, store data in firebase flutter, flutter firestore tutorial, flutter firestore 2023, flutter firebase 2023, how to store data in firestore flutter, store data in firestore flutter, save data to flutter firestore, how to save data in firebase flutter, how to store data in flutter firestore, store user data in firebase flutter, coding with tea, coding with tea flutter, cloud firestore flutter, firestore flutter 2023
Id: LlQimtjqZ9A
Channel Id: undefined
Length: 13min 8sec (788 seconds)
Published: Sat Dec 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.