Using SQLite in Flutter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys GERD here welcome back to another video it's been a while lots going on in the world with all this Kovac 19 stuff so first things first I want to say I hope everyone is staying safe wherever you guys are from wherever you live hope your families are safe I hope you're safe I hope you're making good choices and keeping yourself out of harm so that you can continue to learn and just just be you so in this video what I want to do is I want to go over how you could use sequel light inside of your fluttering application and the first question you might have is well Garrett why why do I have to do that I mean I have to learn flutter which is already a big thing I mean flutters no joke there's a lot to it and then not only am i learning this new framework flutter but I'm also learning this framework that uses a language that I've probably never even heard of likely don't even know how to use something like that right that's Dart that's the language and now you're telling me I have to learn sequel or sequel light if you didn't know sequel before and the answer to that is no you you don't have to learn sequel you don't have to learn sequel like you don't have to use sequel like with flutter the reason you would want to do that is if you need some sort of persistence data wise data persistence even after the application is closed so you might not need to do that actually even if you need to do that the data persistence you might not need to use sequel like because if it's not complex data you can use a simple key value store in which case the shared preferences package will work just fine for you if you don't want to do sequel anything and you want to use a no sequel database there are a number of most sequel databases that you can choose from that you can use with flutter but if you feel more comfortable with sequel or if your data requires it or doesn't require it maybe it you know kind of just makes sense and structured data you have to do a lot of querying a lot of joins things like that not query but just to join then it's structured data then it makes sense to use sequel and in this case sequel like since that's our option for flutter now if you're new to fly you might be thinking to yourself well what does that mean I mean Fleur has you know the the provider package so can I just use the provider package and then you know it'll work right well not quite because well let me show you an example let's say we have this app here it's an amazing app I don't know what it does but it's a great app and inside of this app we have our widgets there right that's that's what we get to see so that makes sense we have some assets maybe some videos or maybe pictures or I don't know whatever it is and inside that app as well we have data well we don't actually have the data yet because we just kind of we haven't actually gotten the data yet but we have like the space for the data maybe the data is user entered data or maybe we reach out to this you know API and we get some data from a server or some database you know if we're using firebase or I don't know wherever it is right but the data the space for the day that we know it's gonna be there right so right now the data is no it doesn't necessarily equal no but it doesn't exist yet so for lack of a better way of illustrating that it's set to null and as we use the app the data now equals one two three four five an array one two three four five now this is great everything's working really well our app works beautifully people love it we get a lot of five-star reviews maybe I don't know something like that but what happens when we close the app but I don't just mean you know close it and come back to it I mean like what happens when we really close it like maybe we gotta do go to do something else or we restart our phone or whatever it is well now it restarts because it was it was it was stored within that application state so now the data is gone and we don't want that we want this data to be there the next time the user accesses the application maybe we're thinking of checking to see if the data exists and if it doesn't we go and pull it from the server and if it does then we don't have to pull it from the server but either way that's not ideal right we want the data to be there when we want it to be there we want to be able to save data so what do we do that's received what light comes in well any database really but in our case sequel light what it allows us to do is separate the data from the application so now we have this database that's stored on the actual devices filesystem right because sequel light is actually just a database inside of a file so it's a database that's stored inside of the user inside of the devices filesystem that basically we can just access whenever we need to once the data is there and so that's exactly what we're gonna do in this video we are going to set up a small sequel light database on our flutter app actually on the app that we've been kind of working with throughout these videos and we're going to basically do the you know the normal thing we're gonna have the ability to add a username and password or insert it and then we'll have the ability to display on the next page so let's jump on over to the vs code and we'll start going so the first thing you'll notice is this is in fact the the application that we've used in the past right the difference here is I took some stuff away and actually I didn't update this page yet so I'll have to go and do that but I took away whatever was here the counter and I added you know this username thing and then tab doesn't work by the way and then I added the password and save and so well type in our username we'll type in our password and then what save it'll get saved in the database and then we'll be able to go to this page which is the other page and we'll be able to see what the user name and what the password are right so let's do it all so the code will be up on I'm gonna run a medium article on basically how to do this and not just showing the code but actually going over how to do it so if you guys would rather actually see the medium article or read the medium article go over and check that out link will be in the description if you just want the code go over link will be in the description as well also before we get into this I just want to mention that I have a survey for my channel that basically just lets me know how I'm doing it but you guys would like to see and if you like these videos or not so please go over that's also a link in the description check that out let me know how I'm doing I really appreciate it it would really help me out so thank you alright so the first thing that we need to do is we need to install the sequel Lite package we need to make sure that we have been come on over here to dependencies and we will enter a sequel Lite right down there that's the first thing we have to do now we actually don't need to be in there anymore so we can't get out of that file that just did its thing awesome now what we need to do is we're going to create a new folder called utils and inside of there we will create a new file called database done dart and now perfect we're inside of the spa so let's start creating the database basically this DB provider which is gonna allow us the ability to access all of the functionality for our database that we would need without having to write it over and over again so let's go ahead and do that we're going to import package sequel actually a sequel sequel F light slash that I will copy and paste this again so they don't have to retype it and we want where as it here the API for right now that's going to be it but we might have to add one more while we're going through this not totally sure so now we're going to do is create a class and we will call this DB provider okay now DB provider DB provider the constructor is gonna be like so or actually next thing we're gonna do is static final DB provider and then we're gonna have a database variable and this will be equal to DB provider like so we're gonna create one more here static database and this I believe comes from sequel Lite and this will be set to equal or set this is a variable here of type database with underscored databases being what it call it being what we call it the next thing we're gonna do sorry I have like a notes over here so I have noticed over here that's why I keep looking away we're now going to create a future which will return a database object and make it a getter which would be database and every asynchronous now this is going to be something called a singleton now we're not really gonna be going over with a single thing in isn't how it works but basically what it does in a nutshell is when you call this file right it's going to check to see if an instance of this file already exists if it does it's gonna return that instance and if it doesn't it's gonna make a new instance of this object of this class all right so now let's now that we have this being called here let's come on down and we will actually create this method here that we're gonna be using to create our database or initialize our database so we will call it the knit DB it will be an asynchronous method and there we go the first thing we're going to do is return oh wait and we will open the database and this comes from sequel of light actually an inside of open database this is basically where all the stuff that we have going on happens so let's just really quick at a semicolon there otherwise we'll get an error so Reggie right now is we're gonna join this together we want to create the actual file that we're gonna have server call join this will be a wait again and then get database path and we're gonna call this YouTube demo dot DB that's what our database is going to be called basically and then instead of there we're gonna have create or an on create this is an event that sequel Lite gives us so that every time our database is created we can create these tables or create the association's or whatever we want to do we can basically have the database run or be created in a certain way so that's we're going to be doing here so we will have the database and the version this is going to take a method so we have database and a version that is taken as as the arguments to this method right join also I believe we have to have import I think we need the path is a dark path my path slash passive path slash path and that's remember when I said we might have to import one other thing that's the one other thing for right now basically this join method comes from the path package so we need to basically add that in okay so this will read you now this is where the fun stuff happens as far as sequel Lite goes now to be clear I believe you can use the sequel Lite package as an ORM to be able to do all this stuff and use its methods and all that stuff I personally don't really like doing that and the reason I don't like doing that is well there's nothing wrong with doing it in fact it's probably a good thing the reason that I didn't do it is because I feel very comfortable with sequel itself and so I'm going to be putting in raw sequel if you don't feel comfortable with sequel but you don't know sequel then two things number one it might be worthwhile for you to go and learn how to use you know all the methods and stuff the sequel like gives you if you don't know sequel already I personally would highly recommend learning sequel not just so that you can do this but because sequel in general is a really really good CEO to have if you don't want to do that there's nothing wrong with using sequel Lite like the helpers that they give you but just know that there's a really good chance that eventually you come across a time where you do need to learn sequel and you end up learning it as such so you might as well just do it now because it will help you in the future especially if you're going to be using a sequel database for sure okay so what we're going to do now is we're gonna have another wait DB execute which is a command again from sequel Lite and whoops we're gonna basically have this on multiple lines here so what we're gonna do is we're gonna have some sort of quotes here I looked at talk to the wrong thing some quotes here and this is gonna allow us to have a multi-line execution command right so we're gonna have create table and I'm going to call this table users and then inside of users we're gonna have our username and our password and our username is going to be of type text once that's the capital it doesn't have to be kept a little thing but I want to be it'll be the primary key which doesn't really matter for this video but you there anyway and then the password is also going to be text and for right now that's all that we want the next thing we want to do though outside of this method is we want to set the version equal to one I don't know too much about the versions and stuff but I believe it has to do with migration things like that since I'm still in development of my app I haven't actually done too much with versioning Naumann I do believe it's back the next thing that we're going to do now that we have this this method basically created is we're going to go and actually create our methods for using the data so inserting it and then retrieving it so what we're gonna do now is create a new user method and we can call this clips new user that's what it takes in so we'll say a new user right this will also be asynchronous and inside of here we will create a DB variable and this DB variable will basically go and call database and if you remember we created this database thing up here this database initializer here so that when it calls it it's going to go and call this it's going to get the database and it's going to say again if the database which is this variable here equals null or does not equal null then just return the instance of it which is the instance of the database if not going create the instance which then runs this init d beef method here which actually creates our database and then return that database right so that we know that we have a working database that we can work that we can use so inside of there we're then going to have a variable and we'll call this res and this will be a weight again and this will be DB which is this DB right here and then we'll say raw insert this again will be a multi-line thing so we'll go like so and we will say whoops insert can't type today it's too early insert into users here we'll have a user name and we'll have password will then say the values actually I want that to be capitalized values and this parts kind of cool we can just put in question mark here because this data is going to come from the new user thing so we don't actually have data yet so these question marks here are basically going to be replaced by this data here we'll say new user username and new user dot password we get an error up here let's see what that's about too many positional arguments but three found that's because this should be an array that's my bed and then that goes away okay so now once we're done doing that what we want to do is we want to return the response and that will be it the next thing we want to do before we actually go and try using this is want to just get some more line breaks in here make a little bit easier to read is we're going to need to get the user right we can put the user in we need to be able to get the user out when we go to the next page so we're going to create a future and this future for right now make it easy will return something that is of type dynamic we'll call it get user sounds simple enough it'll be as a sick again and then again we need our DB so final database right and then we will have the response or we will create a variable this home this bit here which will equal DB oh sorry wait also this should be a way to a wait DB query and we just want that query user now this is going to give us the entire thing and yes I realize I'm giving a help I'm using a helper here from sequel Lite but we're simplifying things we'll be able to see how to do it kind of in two different ways so so we have that so next thing we're going to do is we're going to say res or if res length is equal to zero we want to make sure that we you know get something back then return no otherwise we want to create a res map which will only basically give us the first one so we want to say res give us the first one and then return that one so so long as it's not empty so res map is not empty then return res map otherwise return you know you might be tempted to think well this should work because I'm passing in the right data but we have to actually add something else and that is a model so we're not technically done yet so what I've done here is I've created a very small model which basically is a way to predefined the data that you're going to be passing it or that you're going to be using in not just in this context but in any context and program so we're not going to go over writing this actual code because it's not really what this video is about but if you go to the link in the description to my medium post on this on this topic you'll be able to find the code for this which you can download or copy and paste and it'll work just fine so now we're going to do is we're going to go and start using it so let's go up here to the top and we will import package not that we're gonna import package this is called YouTube demo slash models and then slash users so create another folder called models make a user dart file in there and then copy and paste the thought that a contents of that file which is on medium and then you can come back and it's good just continue on we're now going to specify that this will be of type user now the problem is that what we're just passing in kind of like random data so we're gonna do is we're going to create a new variable and call it this new DB user and we're going to set it to user which we now have to import so make sure that you do that so that should be imported here too right we're going to create a new user instance or instance of a user and this is pretty simple it's going to be user name and this will be a new wait where this come from this was actually think this is just a username right we just store it as a variable here and then we will have password which will now be password and then what we're going to do is we're going to pass in new DB user let's give that a go so use your name password or like save nothing happens that's because we shouldn't have gotten any errors that should have just I should have just worked so now let's go to our other page and let's go try and you know like get this data here so let's um we're up top here let's remove this stuff let's now create a new variable which would be of type map with string no string and I should also be a string and then we will say this is a new user and it'll be empty for right now let's create a new very or a new method which is actually just gonna be override so we'll say init state right so we have that what we first need to do is create a future which will be the user user of future like so and we'll say that user future is now going to be equal to a method that we're going to create called get user so let's go and create get user get user will be an asynchronous method and we will say that we have a final which is user data equals a wait DB provider DB dot get user and then we will return this here so we're going to do something also that's not really part of this video but it is I think technically the right way of doing something and that is that we're really quickly going to create a future of builder and that basically is going to let us wait to build the stuff on the screen until the data is actually there because otherwise we could be querying from our database and before the query query resolves we could be returning some third showing something on the screen and then it never gets shown because what we didn't wait for the right time so it's going really quickly or kind of quickly create a future builder so we can actually getting rid of all this stuff we're just going to go and make something from scratch so this will be a future builder the Builder will be for the future rather which we want first will be the user future that makes sense at a comma there this now will take in a method which has our callback function which has that and the user data do this great okay now we're gonna do is type a lot of stuff so bear with me we're going to be doing this based off of user data connection state and then we will have a case of connection state dot none I'll just do this again once you're doing four actually I think I did a video on future builders so this would kind of be the same as the video that I did we will have waiting we will have active and I think this one is done in all cases actually you know what tell you what we'll do we'll just have it fall through and go straight to done making a little bit easier and we'll say if if the new user dot contains key username so if it does not contain the key username right then what we want to do where is this go up then what we want to do is you want to set new user to be equal to map string string dot from user data dot data okay all right we haven't over there but let's see if we can resolve it by doing this we will return a column which will have some children children which will be a type widget right we will have text and in this text I want this to be new user dot username actually that doesn't exist so it should be like that and then the next one basically just be a copy off of this except that it'll be for password right and then because that wants us to return something we will say return to tainer now right now we're gonna get an error and depending on how I end up this video you might have already seen that error what happens is that if I type in a username like G love and then a password such as one two three four five and I had save that worked that successfully is now saved in our database but we have no way of seeing that because when we go to this page we get an error and the first error we get actually is that this table doesn't exist and that's because when we were creating this I made a mistake and called it users and then tried to pour you from a table called user which doesn't exist so let's stop that and then rerun it this will take a minute or something like that and then we'll be able to get to the next error that we're gonna have and I'll show you guys how to fix that if you try to put in the same again it will break and give you an error talking about a unique constraint that it violates so just heads up don't do that or if you want you can go and uninstall it by doing this deleting it and then it'll basically work again but that kind of takes a while so just use a different username it'll probably make more sense in the long run I'm gonna make my username love and then I'm gonna make my password one two three four five so awesome two users have the same password would save that works awesome we'll go here to this page and it actually it worked but we still got an error here in the console so what's that about let's go over here and let's add some other stuff to this we'll say return container basically right now what's happening is that we have fall through no matter the state of the connection and so even if it is no connection or if it's waiting or whatever it goes right through and we don't want that we actually want to do something different based on the connection and what happens is that as the connection changes it'll rerun this and then it'll show what we want to show so let's go here to return actually shouldn't need a break although for some reason in my code I have a break but return again will be a empty container and that's it let's restart the state I'll try that again this time I'll make my username deal of eight old username I used to use and I'm making the password again one two three four five we'll hit save we get an issue we need constraint and the reason we got that issue is because I've accidentally just hit the button twice actually that's why that happened and why after you stop this and restarted again unfortunately all right so just redid this and now what we'll do is we will make another user called Garrett love eight make the password again one two three four five will it save that works will go done here we don't get any errors now this doesn't give us the data that we want and the reason is because in here we are returning the first user and only the first user that is in the database so what we'll do is we will stop this we'll go and delete our application this will take a little bit longer to run and get on to the device but we'll be able to now see this from start to finish and see that everything works to round out this video all right so everything is now good to go let's make the username gee love and we'll make the password one two three four five five four three two one well it's SAV done let's go back here we get no errors and we can see that while it's really really tiny it did actually work and I'll I'll zoom in there so Garrett when you're editing this note to self zoom in there but uh yeah this works so that is how you use sequel to basically oh actually one more thing I want to show you that this this did literally work because remember what I told you the whole point is that even if we quit the app we should still be able to go back and actually see the same data and that was remember that was the whole point that we were trying to to address you this the whole thing we were trying to achieve was when the app closes we still need the data to be there so let's check right now and make sure that the data is still there on the other page so that we can see that sequel light does in fact work the way that we needed to moment of truth and it still works awesome how cool is that guys so if you guys need data persistence I highly recommend going with sequel light sequel in general is a great type of database it's really really awesome really really powerful highly recommend it if you don't know sequel I'm actually thinking about making a sequel course so let me know down in the comments below would you guys be interested in an entire sequel course if I made one and as always thank you guys so much for watching let me know also down in the comments you know how this video was check out the links in my description because we will have all the code to this paper to this this video will have a written version of this video on medium and what else oh the the survey from my channel which would be really really helpful if you guys would do me a favor and fill that out so what are your so thanks for watching I will see you guys in the next video subscribe if you liked the video and have a good one also stay safe for all the kovat 19 stuff so see
Info
Channel: Garrett Love
Views: 47,092
Rating: undefined out of 5
Keywords: Garrett Love, full stack, front end, back end, how to code, how to build a website, html, css, javascript, React, Node JS, engineer, Learn to code, Flutter, React Native, Mobile apps, Entrepreneurship, Entrepreneur, SQLite, using sqlite with flutter
Id: xrCBVwYjcvQ
Channel Id: undefined
Length: 35min 52sec (2152 seconds)
Published: Thu Mar 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.