SQLite in Flutter
Вставка
- Опубліковано 31 лип 2024
- This video goes over the SQLite schema management setup we use in production Flutter applications.
Written tutorial:
Timestamps
00:00 - Intro
00:58 - Code Overview
01:27 - SQLite setup
03:06 - Schema creation
08:41 - SQL CRUD
12:26 - Using Database in App
14:37 - SQL Migrations - Наука та технологія
Awesome man, Another again content. Thanks, Man
Thanks! I appreciate it :)
This is the video that i am waiting for thank you so much.
You're welcome! I hope it's worth the wait.
@@FilledStacks Definitely worth it.
I definitely knew you would do it Thank u sooo much filled stacks
You're welcome :) Hopefully it's worth it.
Your tutorials are best and consise
Thank you. I try my best to keep things easy to understand
You are amazing man ♥
Thanks man. Much appreciated.
I love your content always fantastic quality
I really appreciate the kind words and leaving the comment. Thank you
@@FilledStacks Thanks to you for sharing such amazing content
@@workflop4117 😊
This is so cool.
I'll waiting for the serie about doing Offline First with: REST and SQLite and FilledStacks. Great Work Dane!
Thank you. I don't think there will be a series about that. I've shown how I do api, now sqlite so there's nothing left in that set of videos you asked for.
@@FilledStacks You could give an example of syncing between the SQLite db in the app and the API!
@@steveandjodee hey, I've seen this idea before. You don't actually sync between the two. Or at least in my experience of making an app offline. I store some of the data, that I won't want the user to request locally in the db. Then I'll always show that first and then fetch updated in the background and update again. There are waaaaay too many ways to do it and it all depends on the api capabilities. What you can get as a single request and what you can't get in addition what you want to be offline etc.
@@FilledStacks I would love to see tuts for building Offline First app. While I do agree it's an architecture choice, I haven't seen clear example of one of the architectures in Flutter using API and SQLite. Most problems occur when I have to sync with multiple devices or managing multiple offline state API requests when one of devices comes online after some offline time. And simply answering use Firebase is not an option.
@@ebelevics Luckily I've never had to build apps like that so I won't be able to make a video about that. It's not something that I think of often. The furthest scenario i've had to use local storage for is to ensure things are on screen while we fetch the real data. i don't manually do "Sync when connected" , I don't "Sync between devices" if the user has multiple accounts. That's all "naturally" taken care of by my startup logic. Get everything needed in my startup view, and then continue into the app when it's ready. So I basically always show fetched data first unless there's no connection. But then the apps I build won't work so I just tell them that and don't do much about it. Can't order from a food delivery service without internet :D so we can just not handle those scenarios because it'll bring no benefit to the customer or clients. And we do have "sync when connected" functionality, but it's all handled by firestore haha, so won't be able to make a video about that on SQL.
Dane my man you absolutely lifted me from scratch in flutter development... Cannot thank you enough.
And what the hell is this Riverpod thing. Would love to see your views on it.
You're very welcome. It's my pleasure to share the knowledge. I haven't used it yet. I will take a look at it and see how much benefit we can get from it.
I was waiting for this tutorial for so long. it was greatly informative and helpful, thank you. Please make a video on GraphQL with flutter is possible
I'm happy to hear that! and I'm happy that you got some value from it. I'm definitely doing GQL soon. Have to go into a bit of a crunch for a client app so we can deliver so I will probably be back in about 2 or three weeks only. I actually have a GQL implementation for this client so I can share that when I get back.
@@FilledStacks I'll be eagerly waiting for it :)
@@hurbes101 :D
💙💙💙 thanks FilledStack
You're welcome. Thanks for watching.
Thanks dane! 🙌
It's my pleasure!
Thank you Dane.
It's my pleasure.
Thank you so much
It's my pleasure to share it! Thanks for watching.
I really appreciate your work. Keep it up bro. Also do a tutorial on localization. Thanks for the knowledge you are sharing ❤️
Hey Hey, I planned to do one but never got around too it. There's quite a lot of functionality to go through and to add into my package before I can do that. But it's on the list of videos to make.
Thanks, I'm looking forward to that.
@@isyedaliraza Hopefully one day soon
Thanks for this. Also covering Moor or Hive will be great.
Resocoder has a good series on moor so I don't think there's anything that I can add. If I ever use hive in production I will share what I learned, at the moment i haven't so I don't have anything to share about it.
@@FilledStacks yes you are totally right, thank you.
@@h3w45 😄
Merci Monsieur.
:D
you good man
Thank you! Much appreciated.
Pretty nice information here. Can I suggest you to follow up this SQL series on how to back up the DB to either google drive or even better dropbox, and restore the DB back again to a simulated brand new device?.
Thank you. I've never done that, the way I handle that is by storing all the information on the server. The database functionality is purely to provide offline capabilities when there's no connection so we show something and not nothing.
I've never done a DB backup. Basically on start I get whatever info I need from the api and keep it locally.
Damn, so many things to dissect here... personally never used FutureViewModel. Going to look into that now.. very nice Tutorial! Thank you!
Thanks! It's pretty basic, if your view DEPENDS on data returned by a Future then use the future viewmodel. Give the future to the futureToRun function and it'll run the future, set the view to busy until the data is returned. When the data comes back it sets the data property on the model and fires onData incase you want to do something when the data is returned. It also catches the errors for you and sets it to the error object. it also fires the onError function in case you want to do something when the error happens like show a dialog, etc. Very useful. It removes quite a bit of boilerplate.
@@FilledStacks I see, I think I'll try to refactor a bit of my app to see how it goes when I'm done with my other stuff.Thank you for this..
On an unrelated note, I was updating my packages and got this error: Because "app" depends on stacked_themes ^0.2.0 which depends on get_it ^4.0.4, get_it ^4.0.4 is required.
If you have the time, can you update your package requirements?
@@wickedgummybear3104 I can definitely update and push that.
@@FilledStacks It's all working perfectly fine as it is, but I just always feel a bit better when everything is tidy and up to date hahaha. Thank you! No hurries though!
@@wickedgummybear3104 haha, you and me both :D :D It's a much better feeling and migration process as well when you do all the incremental updates along the way instead of a massive refactor months down the line. We think the same!
Hi, how to create multi-tables with foreign key with this method? and in openDatabse, there isn't a path, only a datebase name, do we need a path too? thank you!
Hi, that would all be done in your SQL files. However you would do that in normal sql you'd do it the same way here, if that's supported in sqlite. And the path is the name of the DB.
Great video again! How would you organize multiple models in the database_service file? Would you extract the CRUD functions to a file for each model?
thank you! I'm a bit confused by the term "Multiple models in a database_service file" the data models I usually keep in separate files. When there are multiple actions for each model I do split them up at some point but I've found it more difficult to maintain in the long run that way. Unless each of them has access to the same database, but I like having a unified api for all the DB actions. So that's a personal thing on my side. You could split the actions for ORders and posts into separate repositories if that feels better for you.
@@FilledStacks In the example above you have only one model - Todo. The methods ( getTodos, addTodo etc.) that manipulate this model are in DatabaseService. In case that the app has multiple models, where do you place all the methods specific to these models? Still in the DatabaseService? For an app with 10 models and 5 methods each, it can be a file with 50 methods. I'm trying to get my head around how you organize such a scenario. Thank you
@@cautaro I see, i've done both. Splitting the models between multiple classes and keeping it in 1. I like keeping it in 1 more than splitting it into 10 different classes. makes the unit testing easier and the maintenance as well. It might be how i implemented it but I still liked having 1 file more for the maintenance.
Hi, I have a sqlite database I want to 'secure' it using sqlcipher, is this the correct way, have you ever seen an example of using this, thanks Pierre
Hi Pierre, I have not seen an example of this and I also have never used sqlcipher. I've never stored sensitive info locally so I don't ever have the requirement for a secure db.
Thanks I was working with this package , can we also save the state of the widget like for example a process bar
I wouldn't recommend that. I'd rather use a no sql db for that like hive so that you don't have to update your schema everytime you want to save data from a different widget.
I was trying Hive but Idk what’s wrong with it , the package is not stable though I have worked with before but now it’s not working
Can you create a discord channel where you can add people working in flutter so that they help each other out
@@rishabhsharma3821 Yeah we have a channel on Slack join.slack.com/t/filledstacks/shared_invite/zt-hqvqnd50-trS3YfEsL~2LQXfi9XjFBw I don't like discord because everthing is confusing and you can't follow a conversation. Slack has threads so it works out better.
@@rishabhsharma3821 I was using Hive for a while, but also struggeling. At some point it was not doing something which it was supposed to do and didnt give an error either. SQL is a bit too much power for my needs at the moment so I switched to Sembast. It seems to do the trick.
Next video suggestion how to manage onboarding screens(the screens should only be shown when the user first installed the app)
Thanks for the suggestion. I've seen quite a few videos on that. You just use shared preferences, at the end of onboarding you set a value to true. If it;s false when you start the app you show the on boarding. If it's not you skip it.
Can you please do Wysiwyg ?
How to render properly data from wysiwyg stored in firestore? I mean from wysiwyg web to firestore to data displayed nicely in flutter. Thanks 👏
Hey, I have no idea what that is so I doubt I'll be able to make a video about it.
I'm having a issue with my flutter code, Where can I ask ?
You can join our slack channel and ask there join.slack.com/t/filledstacks/shared_invite/zt-hqvqnd50-trS3YfEsL~2LQXfi9XjFBw
have you considered using the hive package it saves much boilerplate and it is fast
I have but the query functionality is limited. I needed the query relations for the data that was being stored
@@FilledStacks i had the same problem but i solved it by adding adapters to each model it should work fine
@@ajnaf7277 Thanks for that info. I'll look into that again when we get around to our next project. I Appreciate you sharing that.
Any experience with using Floor? What are your thoughts on it?
None, I've never used it. The furthest I got was watch the @resocoder video on it to see what it's about. But I've never used it in production.
Unfortunately is not possible to use SQLite in Flutter for web.
If I had to choose a db to use I wouldn't go with SQLite for local storage. Something without schema management would be my first choice. If I had to do advanced queries then it would have to be sqlite other than that you can do with a noSql db that has browser support.
is this technique valid as of today july 2021 ?
Yes. still valid. we use it in production.
Hey, my database is getting deleted whenever I hot restart or also when the app is removed from recent apps and opened again. Any idea how to solve this?
Hey, I don't know why that would be happening. I use the sql_migration_service and it solved that for me.
@@FilledStacks I got the solution, thanks for replying though!
@@jaineelmamtora8208 Awesome. What did you have to do?
@@FilledStacks Well, I was not querying the database properly.
@@jaineelmamtora8208 aaaaaah, that can happen. I'm a SQL noob and that has definitely happened to me before.
Great video, could you please share the source code for this tutorial?
Thank you. It's with all the other tutorials at github.com/filledstacks/flutter-tutorials
Can we use Moor here? Are there any downsides of using Moor?
yes you can. I don't know of any downsides, haven't used it before.
@@FilledStacks you should check it
@@abraiyan7984 I don't need SQLite outside of the basic functionality and I needed a bit more manual control over models and mapping of the data. But i have seen the reso coder series on it.
@@FilledStacks Thanks, man. It will be great to know what kind of database you use for your projects. And I have a request. Make a video on the NoSQL database or a series of videos.
@@abraiyan7984 All the code I share comes from production solutions. So at the moment we're using sqlite. The only NoSql db we use is through firestore which we're using for 2 apps at the moment, another 2 a few months ago. I'd highly recommend fireship.io instead of my content for no sql db content. Jeff has a way better set of knowledge around it. I only learn what I need to to overcome a problem at hand.
Please make a video about handle API and exception and other concept related. I get stuck with this so long :(((
Hey, I handle mine the normal way, I surround the call with a try catch and print out the errors in the catch statement.
many problem around they such as many different responses corresponding to many status codes, how catch it and print error message inside different json type ... :((
@@nero9061 I check for certain status codes and then handle it according to how my app should handle that based on what it means coming from the api. I just add if statements and then do what needs to be done per response code. I have one function that handles all the actual requests so I can handle the response codes in one place and use that function for every endpoint we have.
@@FilledStacks but when i handle in one function actually making request, with one responsebody, I have to call fromJson function in different classes to decode to have actual message to show in dialog, and in every classes i said, the model class is totally different. So I can't return the actual message I want to show from where making request to the view model to show dialog or something like that. Still so confused ...
@@nero9061 Sorry man, i have no idea what you mean. You can just return your response in the way you want it. If you're handling the same response differently in many places that should work fine as well.
Reso is awesome hahahaha it's true
very true :)
Bruh, you rock!!!!!! Thank you so much for this freaking good video!
Bruh, I've gotta question:
Is your website: "www.filledstacks.com" backend and front-end made with flutter for web?
Thanks.
Thanks!! There's actually no backend for FilledStacks. It uses all locally hosted data. The tutorials are generated from markdown files and stored in a local graph ql database. I use Gridsome for my website, it's built ontop of VueJs.
It's pronounced "ess cue light" according to Richard Hipp, and "ess cue ell" for the abstract language (and *never* "sequel" yuck), regardless of how you've been hearing it all these years. Monty Widenius insists on "my ess cue ell" for the app/framework later named Maria, and I've heard "ess cue flight" for the Flutter package.
Thanks, i do always struggle to find the correct ways to pronounce things. I like using the SQL naming SeeQuel Lite. But now I know it's wrong haha.
lol u still on Earth thought u moved to Mars heheh...
haha! Nooo, I love earth too much. :)