**Making Changes to the App**
We're now taking a user that we are then now rather than just calling that the uh sorry post list with post we're going to pass it a user inside of post list we've now added a user so we can store that here and that way now we can check inside of post.userlike.contained which is down here if the widget dot user.uid is contained in the list of people that have liked this post so if it is that means we liked it and then we'll obviously display that green icon.
**Checking the Login Functionality**
One last thing we're going to need to do we need to go to login and rather than calling my home page here with user.displayname we're going to call it just with user so we just passed user now and i think that should be all the changes we need to make and let's go ahead and actually run this app and see if that's working so i'm going to press f5. Doesn't look like we're getting any. Alright, so this app is loaded up i'm going to press sign in with google and now i will simply press my google account to sign in and let's see all right we get brought to this message page let me type hello and when i do that we can see this message actually gets added here and it's working.
**Fixing the Like Button Issue**
Let's see if i can like this. Doesn't look like the like thing is working so i might have made a mistake here and it does look like i did because there was an exception thrown. We'll have a look at that in a second but let's go to the database and see if anything got added ah so i don't believe anything got added i think that is probably because of the exception so i'm going to have a look at this exception.
**Debugging the Like Button Issue**
i've determined what the issue was here essentially when we press that like button we were having an exception thrown. So let's have a look at post.like post and actually see what that is okay so here we are we can see that this actually accepts a firebase user but when we're here and we just pass post.like post.post we're not actually passing um sorry the uh the argument that we need to within here.
**Fixing the Like Button Issue (continued)**
So what i'm gonna do is simply change this to be an arrow function and then where it says post.like post inside of here i'm to pass i believe it's widget dot user so since we have access to user up here what i need to do is make sure that actually when we pass the callback function we pass a function that's going to call like post with the user that liked that post. So we just make a little arrow function which says okay you know this is kind of an anonymous function that this function will simply call this function so when we call the callback it will call post.like post and pass the argument widget.user.
**Testing the Like Button**
Let me refresh quickly here and let's try this again and let's see if things are working. So sign in with google i'll choose my account all right and then let's just go hello and there we are okay so i don't see any errors right now doesn't look like anything's gone wrong. Let's go ahead and like it and we can like and unlike and see that that is working.
**Testing the App**
Now i'm actually going to go over here to this message app and it still seems like nothing is showing up here so give me one sec i'll have a look into this and oh sorry after i refresh the page there we go things are showing up so here we go we can see that we have posts and then we have two posts actually added right here so we have timor sika who said hello another tabersica who said hello.
**Testing the App (continued)**
And now if i go back to my app actually and let's have this open at the same time so we can kind of see this and let's go test and let's hit enter. We can see that this gets added to the post so this is kind of the tree structure i was talking about. We have posts which is kind of like this one of the branches of the tree and then within that we have all of the different posts that have their unique ids and the way we generated these ids was by calling that push method from that database file that we had.
**Conclusion**
Okay so that's the basics of that obviously none of this is showing any of our users liked that's because we haven't liked any of these posts but see if i liked these posts nothing's changing. So we need now a way to actually update these posts that when we like it we can tell that we've liked this post.
"WEBVTTKind: captionsLanguage: enhello everybody and welcome back to the flutter tutorial so in today's video we're going to be working with databases and storing all of the content in our app in a database this may take more than one video we'll see how much we get done now but the first thing we're going to need to do is actually just set up a database on firebase so we've done most of the setup necessary for all the firebase stuff already we've connected our app to it so all the hard stuff is done what we need to do now is simply go to this database tab from our firebase console on the left hand side and then for you guys you won't see this page you'll probably see a page that says create new database or there'll be some buttons there and there'll be two different types of databases you can pick from either a firestore database or a real-time database when you're clicking create new database if it gives you the option to select them select a real-time database so somehow create a real-time database when it's asking you to set that database up set it up in test mode it will ask you do you want to put it in locked mode do you want to put it in test mode select test mode if you've done that you should be brought to a page that looks something like this if for some reason you're here and you're like hey how do i change between firestore i can only see that there's a button right here where you can select between cloud firestore or real-time database obviously right now i'm in real time database but once you get to this page and you've created the database click on rules and make sure that read and write are set to something that's going to evaluate to true you guys might see something that says like now is less than and like a bunch of numbers if you see that that's totally fine what that is saying is essentially hey we're going to let this database be readable and writeable while the current date is less than some date in the future so it's saying pretty much that in 30 days or whatever the data is that you guys have there we will lock this database and make sure that no one can read and write to it and you have to come back and manually change the rules the reason for this is it's very insecure to have your database with read and write as true that means anyone who has a reference to your database can read and write from it meaning they can look at accounts they can look at information like everything right so if you're putting this into a production app then you're not going to have these set as true and you're going to have to do kind of a more advanced authentication since we're just learning right now and this is for tutorial i don't care we're going to leave this as true but if you are planning on putting this in production note that you will have to change these and do a few other steps as well okay so now that we have that i'm going to go back to data and i'm just going to leave this kind of minimized in the background because we will be referencing this page just to make sure that everything is actually working all right so now that we have that we're going to go into our flutter application and the first thing we're going to do is go to our pubspec.yaml file we should be familiar with this by now and we're going to add in sorry i was in the wrong project folder there i'm in the right one now but sorry go to our pubspec.yaml file and then what we're going to do is add one more dependency which is actually the firebase database now i have to find what version this is okay i'm just going to copy it in from my other screen here but this is it so firebase database and this is going to be version 3.1.6 so add that into the dependencies and then save i know you guys can't really see the pop-up because my face cam is covering it but there is a little thing that's saying flutter pub get was being run as always if that didn't run then you can just go to the command palette and then go pub get packages and that will look through the pubspec.yaml and make sure that you have all the packages that you need but assuming you added this in there and you didn't see any errors popping up or anything we're good to go and now we're going to make a new file inside of our lib folder here and i'm going to call this one database.dart all right so the first thing we need to do here is just import a few modules so we're going to say import package colon and then we're going to go firebase underscore database slash firebase underscore database dot dot so that's the file we need there and then the next one we're going to do is we're simply going to import post from our own file here so we'll just say import post dot dart now the reason we need this is because all of the database related stuff is going to be dealing with the posts so essentially you know saving posts updating posts oh a user liked that post we need to change how many likes they have so i'm just importing posts so we can actually use that class and then what we're going to do is set up our database reference which is actually going to reference the database and i'll kind of talk about how this stuff works but we're just going to call this database reference like that if i could type it properly and that's going to be equal to a firebase database dot instance dot reference so what this is doing is saying okay we've connected to firebase here let's get the firebase database instance let's get a reference to that so now we can simply use database reference whenever we're looking for a certain record in the database so if i have a look here excuse me sorry this is the record i'm talking about when we ask for dot reference here that's going to give us a reference to this and now that we have this we'll be able to see anything with within it so essentially if i click new here you can see i can add a name and i can add a value that's data that's being stored inside of this so if we have a reference to this then we can now add data retrieve data you know all that fun stuff anyways this will make more sense in a sec but that's what we need for the reference next what i'm going to do is i'm actually going to sorry show us how we can add something to the database so to actually save a new post what we need to do is we need to make a function here that can simply add that to the database so i'm going to say database reference because this is what we're going to return save post and then this is going to take a post like that so what this method is going to do or what this function is going to do that i'm writing inside of here is simply take a post object do some stuff to it and then automatically save that in our firebase database this is just the first thing i want to test out to make sure that everything's working and yeah okay so first thing i'm going to do is i'm going to say var id equals database reference dot child i'm going to do posts slash and then i'm going to say dot push now i know this makes no sense to you so let me explain database reference is the reference to our database the way that this kind of works is there is children in the database which are kind of like the different sections of items almost right so we could have like users posts photos images comments like stuff like that kind of the different database tables this works different than your like standard database it's like a really simplified form but you can imagine that underneath here there's kind of a tree of different objects so maybe we have like a post object and then within that we have all of the different posts maybe we have like a comment object and that has all of the different comments so when i'm saying database reference dot child posts what i'm doing is saying okay i want to reference something called posts within the database now right now we don't have anything called posts right there's nothing in here that we can see that is called posts so what this will do is automatically create this new reference called posts within the database now the reason i'm doing this slash is i'm pretty much saying hey i want to reference a child that is kind of like a folder right that's that's not one individual object but that holds multiple objects so just like if you're talking about like a path right and you have like you know desktop and then maybe in our case i had like flutter as a folder and i did another slash then that would tell me oh hey this is a directory this isn't a file so that's the same thing here with post we're saying that's kind of like a directory to where the posts are and dot push is saying okay so every time that we add a new entry into posts we need to give that entry a unique id so just like any database system you might have worked with before every single entry in the database has to have some kind of unique identifier that allows us to differentiate it between something else because technically you know the same person could post two different posts and they could have the exact same content and if we didn't have any unique id for them we wouldn't have any way to differentiate between them right so we need a unique id so what this is going to do this push is going to give us a database reference to a unique id that we can store inside of posts so all we have to do now is take this id and set that kind of like object that's in our database to hold the values that we want so i'm hoping that's kind of making sense once i add this in you'll see a lot more clear how this actually works but now i have id which is kind of like a placeholder item in my database and now what i need to do is just set the content that this is going to hold or set the data this is going to store so i say id dot set and then inside of here we need to put a json serializable object now if you're unfamiliar with json that's totally fine but essentially what we need to do is we need to take this post object that's being passed into us we need to kind of serialize it into a dictionary that has key value pairs so rather than having you know like actual attributes on the class we're going to have a dictionary that says body okay that's equal to this author that's equal to this likes that's equal to this yada yada yada okay so what i'm going to do actually is modify this post class a little bit just because we're going to need to kind of change how things work based on how this database goes uh and actually we'll do that first and then i'll get into how we actually serialize this into something that can be stored in the database so let me zoom in a little bit okay so we have body author likes and user liked first thing i'm going to do is get rid of this user liked the reason for that is that we had that before just to tell us if the current user had liked this post or not but now when we have multiple users using our app that's not really going to mean much to us if we have this true or false value and you know we load that in on two different people's things essentially what that would mean as soon as one user only one person liked this post no one else would be able to like it because we don't have a unique way to determine which user liked the post we just know it if a user liked the post so rather than saying user liked what i'm actually going to do is completely change likes and i'm going to say instead of into likes i'm going to say list users liked equals an empty list and sorry actually we're not going to make this a list i'm going to make this a set and we'll talk about why in a sec okay so now i have a set and what this set is going to store is all of the ids of the users that have liked this post so that way i can tell how many users have liked this post based on how many entries there are in this set and i can see oh has this user liked this post already well if they're in the set they have if they're not in the set they haven't and that'll be the way we determine whether we add a like or subtract alike is just simply by adding users to the set or removing users to the set so now instead of like post what i'm going to do is say if this dot users liked dot contains and we're going to need to now take a firebase user user so what we're going to do is pass actually yeah we'll pass a firebase user user into like post which says okay well if we like a post you need to give me the user that liked it and then we're going to see if this contains the user.uid so what we'll do is if it does contain the user uid then we're going to say this dot user dot remove and then user dot uid otherwise we'll add the user id into the set so this dot user liked dot add user.uid hoping that makes sense uh but that's kind of the basics that's how we're going to do it with the likes we're just going to have a list now so we've actually shortened out the amount of attributes now the next thing i'm going to add on here is actually an id so i'm going to say string underscore id and just leave it like that now the reason i'm doing that is because since we're now storing this in the database all of these posts need to have some kind of id associated with them if i want to update this post well i need to know what post to update right so i have to have an id on it so that i can actually reference the thing so instead of actually just having id i'm going to change this to be a database reference and what this is going to do now is let me store the reference in the database to this specific post object you know we won't display this anywhere on the screen but we just kind of need this in the back end when we're updating objects so now what i will do is i will make now the a method that will allow us to change the id so i'm just trying to think what i want to do next but what i'm going to do is i'm going to say void set id and what i'm going to simply say in here is we'll take a database reference id and we'll do this dot underscore id equals id next we're going to make a method that can actually transform this entire post into a dictionary so i'm going to say map string and oops and then dynamic and what this is going to do is be it's going to be called to json and it's going to return a string sorry a map which is just a simple dictionary that has string keys and dynamic meaning pretty much anything as the values associated with those keys so to json like that and now we'll simply return and we're just going to make a dictionary and you'll see what i'm going to do in here so i'm going to go ahead and i'm going to say author like that author colon and this is going to be author so we could do this dot author just to specify as well so author this dot author comma we're going to say users liked oops users liked and then colon this dot users liked and then finally what else do we need we need the body and then we'll say body and this dot body all right so there we go now we have actually what we wanted to kind of serialize so we're pretty much just setting it up so it's a dictionary right we just have a string key and then we have what the actual value is i know it seems like oh well why do we have to do that but you'll see just for us to store things in firebase it needs to look like this uh yeah so user likes is gonna be a set so what i'm actually gonna do is i need to change this user like to set into a list the reason for that is sets are not serializable uh within kind of like json objects so whenever we have a set we just have to turn it into a list we want it to be a set because it's really fast to perform contains operations add operations and remove operations in a set whereas the list is a lot slower but as soon as we actually want to store this in a database we'll have to convert that to a list so that's what we do right here okay so now we have all of that and you can imagine that if we're now going to store this in the database we need a way to actually take this and turn this back into a post object that we can use internally so we'll do that next but let's go to database i just want to get you thinking about that and let's say id dot set and let's go post dot to json so what this will do is it will take our post object it will call this to json object which will return that map and then we'll save that map in the database and then what we'll do is we'll return that id and that will be pretty much all we actually need to do when we save this post okay great so i think that should be working so what i'm going to do now is just make it so that when we make a new post we'll just call that just to see if that's actually working and we'll keep going from there so let's go to text input widget um actually this is going to be my home page and where it says a new post what we're going to do is when we add the new post we'll also add that to the database so instead of just having new post i'm going to say var post equals that and this doesn't necessarily need to be inside of set state but we can save our post equals new post let's put that above here and then we will say post dot add and we will add that post and before we do that actually sorry we need to save the post so we're going to call save post which is the method that we put inside of database.dart when i called that it automatically imported um what was it database.dart you can see that at the top right there so save post and that's what we need to do and then what we'll do after we save the post is we're actually going to say post dot underscore id equals and funny enough that's going to be equal to save post so let's just do that and sorry not underscore id this is going to be set id and we'll set that equal to say post okay so i think that's good let me just go over this again what we're doing is we're calling save post right we're giving it that post object that we just created up here so now if i actually go to post we can see that save post what that does is sorry that's in database save post what that does is store this in the database and it returns us an id so once we have that id now we need to actually add that so if i can find where this was to this post object so that it knows where it is if we have to update it or something later on and then we go this dot set state and we say post dot add and we add that to the list so let's see if this is working now i'm going to run the app oh build errors let's see what our errors are here okay so we need to make a few changes just because i realized we did modify that post object and i didn't modify where it was being used so if we go ahead and look in postlist.dart rather than going post dot likes we're going to go post dot and this is going to be user liked dot length so we can figure out how many people liked it just by getting the length of that set and then same thing here instead of saying post dot user liked we'll say post dot users liked dot contains and then what we're actually going to need to do is get the current users your uid and see if the current user has liked it by just checking if they're contained in the set of users that have liked that so i don't think we actually have access to the user object in here so that's the next thing that we're going to need to change so let me actually just close some of this stuff here we now need to make sure that we're actually passing in the user object to this so that we can use that so if we look here let's see stateful widget let's now add another field up here so let's say final firebase user like that and we'll just call this user and then inside of post list we'll say this dot user so now this means we have to pass in our user object to the post list so let's go back to my home page which is i believe where we call this and do we have user here no we actually only have name so now we're going to change this as well inside of my home page so we have access to user so instead of final string name we're going to say final firebase user like that and instead of name this is going to be user now this is going to be this dot user and instead of widget.name we're going to say widget dot user dot display name and that should be good firebase user okay it's saying we don't have this imported so let's hover over it click quick fix and then import library firebase auth from there okay so now we have all the packages we need what i did to do that again was just hovered over it there was quick fix and i just pressed import the module that we need okay so now post list postlist is saying hey you know we're missing an argument we that's because we now need to pass the user through so within post list we're going to say widget dot user all right so that should have hopefully fixed the errors i'll quickly go over what we did we just modified my home page so that rather than taking a name we're now taking a user we are then now rather than just calling that the uh sorry post list with post we're going to pass it a user inside of post list we've now added a user so we can store that here and that way now we can check inside of post.userlike.contained which is down here if the widget dot user.uid is contained in the list of people that have liked this post so if it is that means we liked it and then we'll obviously display that green icon so one last thing we're going to need to do we need to go to login and rather than calling my home page here with user.displayname we're going to call it just with user so we just passed user now and i think that should be all the changes we need to make and let's go ahead and actually run this app and see if that's working so i'm going to press f5 it doesn't look like we're getting any all right so this app is loaded up i'm going to press sign in with google and now i will simply press my google account to sign in and let's see all right we get brought to this message page let me type hello and when i do that we can see this message actually gets added here and it's working so let's see if i can like this it doesn't look like the like thing is working so i might have made a mistake here and it does look like i did because there was a exception thrown we'll have a look at that in a second but let's go to the database and see if anything got added ah so i don't believe anything got added i think that is probably because of the exception so i'm going to have a look at this exception i'll be back and i'll discuss what what went wrong all right so i've determined what the issue was here essentially when we press that like button we were having an exception thrown so the reason for that is if we have a look here we can say that we're calling this dot like which is the method up here which just has the set state here for us and we're giving it a callback function which is like post now let's have a look at post.like post and actually see what that is okay so here we are we can see that this actually accepts a firebase user but when we're here and we just pass post.like post.post we're not actually passing um sorry the uh the argument that we need to within here so what i'm gonna do is simply change this to be an arrow function and then where it says post.like post inside of here i'm to pass i believe it's widget dot user so since we have access to user up here what i need to do is make sure that actually when we pass the callback function we pass a function that's going to call like post with the user that liked that post so we just make a little arrow function which says okay you know this is kind of an anonymous function that this function will simply call this function so when we call the callback it will call post.like post and pass the argument widget.user so let me refresh quickly here and let's try this again and let's see if things are working so sign in with google i'll choose my account all right and then let's just go hello and there we are okay so i don't see any errors right now doesn't look like anything's gone wrong let's go ahead and like it and we can like and unlike and see that that is working so now i'm actually going to go over here to this message app and it still seems like nothing is showing up here so give me one sec i'll have a look into this and oh sorry after i refresh the page there we go things are showing up so here we go we can see that we have posts and then we have two posts actually added right here so we have timor sika who said hello another tabersica who said hello and now if i go back to my app actually and let's have this open at the same time so we can kind of see this and let's go test and let's hit enter we can see that this gets added to the post so this is kind of the tree structure i was talking about we have posts which is kind of like this one of the branches of the tree and then within that we have all of the different posts that have their unique ids and the way we generated these ids was by calling that push method from that database file that we had so okay so that's the basics of that obviously none of this is showing any of our users liked that's because we haven't liked any of these posts but see if i liked these posts nothing's changing so we need now a way to actually update these posts that when we like it we can tell that we've liked this post you\n"