Moor (Room for Flutter) #1 - Tables & Queries - Fluent SQLite Database
Вставка
- Опубліковано 31 лип 2024
- 📗 Learn from the written tutorial & get the project files 👇👇
resocoder.com/flutter-moor-ep1
👨💻 Do you write good code? Find out now 👇
resocoder.com/good-code-quiz
Mobile apps and local databases go well together. If you want to take the relational database route, SQLite is the winner. However, the problem with bare bones SQL databases of any kind is writing queries and then integrating them with your other code.
Android solves this with the beloved ROOM library, on Flutter though, you are stuck with the low-level SQFLite package... Not anymore! MOOR is a library allowing you to work with the Flutter's SQLite database fluently and in pure Dart. Behind the scenes, it uses the SQFLite package. Oh, and if you're wondering, MOOR is just ROOM spelled backwards.
Go to the website for more information, code examples, and articles:
● resocoder.com
Follow me on social media:
● / resocoder
● / resocoder
● / resocoder
● gab.ai/resocoder
I don't normally write comments in UA-cam or any other social media in general, but I just wanted to thank you for you hard work and continuous effort in creating invaluable content with just the right amount of detail. Your videos have helped me since I was applying for companies with 0 knowledge of Kotlin, into writing a Kotlin app that earned me my job, beating 60 other applicants who have 3+ more years of experience over me, and now helping me with the transition to flutter -again with 0 experience- and getting credited for delivering the best output at my workplace. Thank you so much.
Reading about your successes is the most rewarding thing ever. Thanks for learning here on Reso Coder!
how did you find out that there were 60 applicants for your position?
2020 UPDATE: The Provider wrapper no longer uses the 'builder' property but instead uses 'create'. So the code builder: (_) => AppDatabase(), changes to create: (_) => AppDatabase(),
thx
Thank you so much
I can upvote just once, but thank you so much (:
I needed this soo much😂
thank you!
from the bottom of my heart, thank you.
Great video 👍🏽. Moor is a very useful package. I do like the Floor package also. It is exactly like the Room library
Great video about setting up the EASIEST database in Flutter! Thanks!
😍😍....why r u so awesome... Thanks for relating ur tutorials to native android development, it's the best motivation to learn👏
Thanks a lot, Moor is more easy, less code writing than Room Android Library!. Thinking to do all my future mobile projects in Flutter rather than Native(Kotlin) way fully.
I'm in love with Moor.. Thanks man!!
This tutorial is most likely the best i have ever seen for Moor Database in Flutter. Thank you very much for your awesome clear explanations and demo's! There are very few tutorials on Moor Database available in youtube and the documentation for the plugin is difficult to follow if you are new to coding etc.
Would it be possible to have a tutorial demonstrating how to obtain data from Moor database into a chart/graph in flutter? That would be awesome to have a tutorial on that to follow on from learning Moor.
Thanks again for your amazing contribution and i would think that many novices out there will really appreciate this tutorial.
keep doing the flutter stuff. some awsome job you're doing there
thanks for the great work man, just what i needed
!
It was awesome. Thanks for the great work you did.
Excellent Introductory Tutorial with Moor
one of the best tutorial , thank you very much sir!
Your tutorials seem great to me! Can you teach about how to do with several tables with SQLite and Flutter?
you are amazing man :)
thanks
This is one of the best software engineering channels on UA-cam!
Thank you, I'm Fluttered 😉😉
@@ResoCoder Lol I saw what you did there.. You are awesome
thats great.. thank you
When you set the withDefault for the BoolColumn, why do you set it as Constant(false) instead of just false? What does the Constant() do? Thanks.
Woohoo !! Great tutorial thanks 😍👍
Great tutorial
What would I need to put in the value for a SwitchListTile? It is driving me bananas.
thanks
good stuff
Now the library is renamed to Drift
Thank you very much man! Can you please share your opinion on how much should one know Flutter in order to get a job.Like the areas that are a MUST! Thank you again!
Question please: if I want to use my pre existed database in the assets. like an offline dictionary app, Can you lead me to idea?
Your videos are always well explained and really helpful to a flutter beginner. I have a question related to local db crud. If in my view, the datas are from db and network at same time, how can I implement it? For example, in the task list, I need show a task emergency level field, which need to use taskId(this Id is still in local db) to query from an remote API.
Great tutorial as always from you - quality work! Thanks and please keep it up. Clarification - in the #2 video, you make use of the "moor" package in your pubspec, but in the final source code, only "moor_flutter" and "moor_generator" are included. Which is correct?
Excellent content, as usual. Are you on Patreon or another creator platform?
i need some assistance. how do i create a database in the code and call it upon pressing a button instead of generating it from a the keyboard?
how would we add an int to this, say I wanted to add age aswell and it be saved to the database, the problem i can't figure out is how to save the input in the ui. With this it is a String name, but how could we add int age also. We have taskname or name, but how could I add multiple lines, say I wanted a ui, with name age, maybe weight and say another string hair colour, how could i do this and beable to update them?
Hi Reso Coder, do you had something that can work another database then SQLite and fireBase such as MySQL, PostgreSQL or MongoDB?
Excelent work. How about relationships? Works with moor? thanks
21:35 You don't recommend Redux because of overengineering and boilerplate?
Moor looks great, I'll have to try it! Great topic, great video!
PS. Would you consider removing the modal popup from your website? They're cancer.
Thanks. They're cancer, but they work. It's a proven thing. I'm actually surprised at how well it works 😄
nice lesson - we can use Chopper and moore to build online offline app?
How can I fill the database with an initial data which is awailable from the first run? With the todo example so that when the user installs the app there are already to-dos in the database. (sorry I'm a beginner hobby programmer)
How do I get a pre existing sqlite database into the framework in the first place. I'm migrating a project, say.
If you are facing the same issue I faced in the video (no value added to the list because the provider was giving an error), set listen: false in the provider.
Hi. When In 21:49, i am trying to keep the builder as is like in your tutorial but i am getting the error:
The argument type 'Widget Function(BuildContext)' cant be assigned to the parameter type 'Widget Function(BuildContext, Widget);
Some one please assist me as to how i can fix this. What did i do wrong?
So this error is causing because of the current version of the package provider. If i keep as same as yours, it works, although it affects my other packages. What is the way forward from this?
@@shabeenabarde Use create instead of builder
@@danielw1979 It works, thank you
very nice, how i can remove all rows inside table?
How similar is this to the Drift update? Would I be able to use this same tutorial or would it be a lot different?
Nice tutorial. How to check api data exists in sqlite database api data in listview, please help.
Muy didactivo Mister Reso gracias..... A que framework recomienda aprender Xamarin o flutter gracias
Hola! I'd choose Flutter for sure.
How can I call a queries function for deleting whole table.
eg :
@UseDao(tables: [ Employees],
queries: {'deleteAllEmployees': 'DELETE FROM Employees;',})
.
or,
For Deleting the Whole Table Rows How to create this type of function:
Future deleteEmployee(Insertable employee) =>
delete(employees).delete(employee);
Hello, I'm facing one issue can give me give me solution issue is : "[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Invalid argument(s): onCreate must be null if no version is specified"
I'm having an issue where I try to add a new recipe to my database but it requires me to add an id in the ui(because it is @required in the generated class). I remove required and it fixes the issue but i obviously shouldn't change the generated code. ps. id is auto incremented in my recipe that i wrote
Any fixes besides changing the generated code or should I try creating an issue on GitHub?
If you are using a more up to date version, use a UpdateCompanion class - see the second part of this series or the official docs for Moor.
Awesome tutorial! What theme are you using?
u found it?
16:21 you are also a good poet 😆😂
Hi @Reso Coder, the newest Provider version seems to be breaking the code, I think it has to do with the implementation. Can you take a look and offer some feedback? Deeply appreciate it!
and need set value from database to textflied
great tutorial!
Please help me with:
i'm getting this error: : The argument type 'Stream' can't be assigned to the parameter type 'Stream'.
and in moor_database.dart i'm getting erro when put this way:
Stream watchAllTasks() => select(tasks).watch();
so i changed to:
Stream watchAllListas() {
select(listas).watch();
}
we need more information
(br aq tbm)
Go to your generated class, see what is the actual name for your TaskData, mine was TaskData, Don't mix data with the table/
What's the main differences between Moor and Floor?
When the update to drift?
Not working.
Provider( builder: ...) gives an error.
I tried to use create: (_) => AppDatabase(), but I get an issue with Provider else where.
I'm assuming you're not stuck on this anymore, but for anybody else the solution is to change line 45 in 'new_task_input_widget.dart' from --
`final database = Provider.of(context);`
-- to --
`final database = Provider.of(context, listen: false);`
-- which should fix the issue.
@@elocnat Thanks. Now it works fine.
Sqlite documentation stated that:
1. The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed.'
2. In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.
3. On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.
My question is can we declare the id (of Task table) without the autoincrement and instead set it as primary key? will it work the same way?
Any update on this? Is this question on StackOverflow?
20:00
I got an error:
Error running MoorGenerator
NoSuchMethodError: The getter 'typeConverter' was called on null.
Receiver: null
Tried calling: typeConverter
I checked the code many times and the code itself should be clean. Also rebuild the dart_tool/build directory. Any suggestions for an fix?
If you are new just use cloud firebase
hi please unable to generate this file 'part 'moor_filename.g.dart' it keeps on showing error eve after running the command flutter packages pub run build_runner
I had the same problem. I created a new project and copy-pasted everything and somehow it worked.
can we prepopulate?on moor
Please help!
when running 'flutter packages pub run build_runner watch', how come I always get this error:
Invalid argument(s): Path must be absolute : dart:core
[INFO] 1.2s elapsed, 1/3 actions completed.
[SEVERE] moor_generator:moor_generator on lib/data/moor_database.dart:
I got that error too as I was recording a new tutorial today and I almost plucked my hair out!
What you need to do is to go to the pubspec.*lock* file (not yaml) and change the analyzer version to "0.38.2". I don't know why they push out broken code, but hey, that's how it is. I hope this solves the issue for you.
@@ResoCoder Oh Wow! Thanks for the reply! Finally build success! Thanks again
ty
@@ResoCoder no bro if i change the version it is saying to do pubget so if we did that again analyser version resets ,tq in advance
How can we set conflict strategy ?
Did you get an answer to this?
Great tutorials!! I tried to get single row matched with valueid but i couldn't figure it out for three days.
Future getBookmark(int valueId) =>
(select(bookmarks)..where((book) => book.verseId.equals(valueId))).getSingle();
Code above i used to get single row matched. Can you please tell me how?
In my terminal was showed this msg after running flutter packages pub run build_runner watch. What is problem?
Dart 2.11.0-260.0.dev is incompatible with your dependencies' SDK constraints. Please run "pub get" again.
pub finished with exit code 65
I found problem before running "flutter packages pub run build_runner watch",to do run "flutter pub get" in terminal.
I have a problem with the streams made by moor they don't notify listeners like in your example I tried with stream builder just like your example and I also tried with StreamProvider and the both work just as getters the app won't show new tasks added unless I make a hot reload the problem isn't with stream listeners it is with moor's stream at first I used the same version in your tutorial then I used the latest version and no luck. I researched about moors streams and not much docs or QAs out there the user base is very low. I just don't know WTH with it. Any ideas?
Hey, I know it's been a long since you commented on this, but have you come up with the solution? I am also facing the same issue.
Thanks
@@varadgauthankar I believe that was my last experience with it, I skipped on it and used different state management system.
@@travelfoodbusinessbestheyyono ohh its okay :), thank you so much for the quick reply really appreciate it, have a great day.
Hi Reso coder once again a nice tutorial from you but why you have stopped the flutter chopper series. We are waiting for the continuation of your chopper series
I didn't stop, next part coming this Saturday!
@@ResoCoder Hey that's great, if it's possible please a video on uploading the list of multipart files using chopper
I will cover everything but it will take multiple parts. The next one will be about interceptors and possibly basic converters.
@@ResoCoder Thank you we are all eagerly awaiting for the chopper continuation.
I found these issues
[SEVERE] moor_generator:moor_generator on lib/data/moor_database.dart:
Error running MoorGenerator
NoSuchMethodError: The getter 'typeConverter' was called on null.
Receiver: null
Tried calling: typeConverter
Got the same thing in my places..dart file
Moor: Sent SELECT * FROM places; with args []
I/flutter (10579): Another exception was thrown: NoSuchMethodError: The getter 'data' was
called on null.
Lost connection to device.
Do you use the .call() after creating the columns? Because you should just remove that and use the () instead
@@WimvanDeursen Hi Wim, I do actually use ()(). I did get this sorted, but I needed to figure out how to change the value of the switch now, which is a bool.
thanks for replying
Hi, can you know somehow to migrate a database made whit SQLite to Moor, or another reference that help whit this? I'm grateful if you could help.
Regards.
Great tutorial!
but i think that "floor database" is the most likely as Room in android .. not moor.
its still in alpha, right? it's december 2020 already..
Is there a chance to combine moor with TDD?
I wouldn't test Moor. I'd just create a layer of separation (a DataSource for Moor) and mock that in other tests, if needed.
I could not generate the g.dart file. can you help me?
Have you typed this command on terminal ? flutter packages pub run build_runner watch ?
@@mrd1gr00tKawHmuNewsApp I can run the command but no file is created: Succeeded after 63ms with 0 outputs (0 actions)...
@@ShadowInfest check in yr code Bro :D i think smth wrong :D
@@mrd1gr00tKawHmuNewsApp y I had a naming which was not allowed in SQL... loop. If you don't know that you search for a while
I would have named it Cocoon
Why the heck is working with SQL is not a good thing. I think if you understand and implement SQL statements yourself it will be so efficient for coding and for the running of the app
I/chatty (24609): uid=10233(com.example.sql_db) Thread-2 identical 1 line
dont't work to me
what is the name of the theme you use in vscode please?
u found it?
@@Malaq4822 nope, i switched to intelIJ xD
Invalid argument(s): Path must be absolute : dart:core
[SEVERE] moor_generator:moor_generator on lib/main.dart (cached):
I got that error too as I was recording a new tutorial today and I almost plucked my hair out!
What you need to do is to go to the pubspec.*lock* file (not yaml) and change the analyzer version to "0.38.2". I don't know why they push out broken code, but hey, that's how it is. I hope this solves the issue for you.
@@ResoCoder you are amazing!
Am I the only one who find the ordinary SQFLite package much more simpler than having to take care of all of these information 😒
Yes
Obviously yes!
Yes
What does nullable mean?
It can be set to null.
@@ResoCoder oh yeah thanks for the reply and the awesome video. I never knew about this package.
Name of Theme please?
u found it?
Is it better to learn this or sembast?
Unless you need SQL, I'd go with Sembast.
@@ResoCoder thanks, why might you need sql over sembast?
I am also hearing about hive, I am a newbie so don't really understand databases much, so which is best to start with?
Hive is probably the simplest one to start with. I'm actually preparing a tutorial for it.
@@ResoCoder that would be good, I haven't seen a tutorial of it anywhere
There is no encryption for Moor wont recommend using it in Production.
Warning database has been locked for 0:00:10.000000. Make sure you always use the transaction object for database operations during a transaction.
In my moor_database.g.dart file the DB has been locked for 10 seconds.
The file generated code example:
@override
Set get $primaryKey => {id};
@override
Task map(Map data, {String tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
return Task.fromData(data, _db, prefix: effectivePrefix);
}
Old and need to be update