Hive (Flutter Tutorial) - Lightweight & Fast NoSQL Database in Pure Dart
HTML-код
- Опубликовано: 30 июл 2024
- 📗 Get the starter project & learn from the written tutorial 👇👇
resocoder.com/hive-db-tutorial
👨💻 Do you write good code? Find out now!
resocoder.com/good-code-quiz
Storing data locally is a task which has to be done by almost every app. Maybe, you want to cache responses from a REST API or you're building an offline-only app. In any case, choosing the right local database can make all the difference in how quickly you can develop the app and also in how performant the app will be.
Hive is a lightweight, yet powerful database which is easy to develop with and it also runs fast on the device. Unless you absolutely need to model your data with many relationships, in which case you should probably use SQLite, choosing this pure-Dart package with no native dependencies (it runs on Flutter Web!) can be the best option.
Go to my website for more information, code examples, and articles:
● resocoder.com
Follow me on social media:
● / resocoder
● / resocoder
● / resocoder
Most underrated flutter-tutor on youtube! Love your work, it's keeping us folks on coding with flutter! Props to you mate!
You're really important to the community, Reso! Thanks!
Awesome. Hive looks like a much more viable option than SQLite to me in terms of simplicity and ease of setup and use. Thanks for the tutorial.
The two best coding channels for me at the moment. Much love and thanks for all the help.
@@rossthemusicandguitarteacher Thanks Ross! I love myself some ResoCoder too :)
Great tutorial. And the same for the others on your channel. Keep up the great work, yours is definitely a really good channel to learn Flutter and various libraries!
Thank you!
This was incredibly helpful. Thank you! Subscribed!
Every second of this video was useful for me. Thanks.
Best channels for learning flutter....thankyou
Great video and very well written tutorial. Good job! I added your video to the documentation.
Thanks for the library again, Simon!
@Simon Leier oh yes wanna thank you also sooo much for thie great package 👏🏼 Greatest feature for me is the possibility to save whole dart objects :)
hi sir, can i store my shopping app cart list in hive thank you :D
Thanks, very well made tutorial and useful. I was considering to switch to Hive earlier already for storing app settings. After this I am convinced and with a good and easy to follow guide.
I'm glad you found this useful, Mike!
Very helpful video, much better then documentations for doing it first time.
Thank you very much for this wonderful tutorial!!
best hive video out there!! thanks :)
I was going full inception, listening to The Hives while working with the Hive.
Great video! Thank you so much.
Ok you got my subscription ... You are being so awesome
Thank you so much for the tutorial 💙.
Nice tutorial and thanks for clarifying some stuff for me. On the build _runner/Dart analyzer thing. I shared the same experience but while creating a generator/build library. I almost went mad.
I feel you 😰
Love your content Sir.....
Great tutorial, very clear and concise. I had question though, Is there a way to implement search with hive??
WOW! Impressed... Thx!
I started today with hive...at the beginning hard to unterstand things like typeadapter and so..i fumbeld round about 2 hours with generating of the .g.dart File..because Null Safety Problems from Others dependencies..but now hive is very fast and very nice to handle, Not Like sqlite3 or so. Thank you for this tutorial which i found just a hour later after i solved much of the shown ways alone or by Stack Overflow 🤣
very useful, thank you
Thanks like always.
Great tutorial 👍
finally new lightweight database !
Nice design dock and window btw ! What did u use ? Rainmeter ? And which package ?
Thank you. What do you think about Hive + Firestore implementation?
Great tutorial. Is there a flutter hive client/data visualiser to inspect boxes?
Superb. Matej, I have somewhat of an unrelated question to Hive. It occurred after you used compact() inside of dispose override. I guess compact() is a sync method. However for the sake of argument, what if it was an async method. Can we use an async call inside of dispose()? I could not find any info on SO. Thanks man.
I solved that analyzer stuff by just using pubspec assist and providing analyzer package that was 2 versions down. That way, when the issue is fixed all i need to do is make the package version empty then run pub upgrade
helpful, Thanks for sharing
You are so good.
Thanks boooy your the best
Hello
Ahmed from Egypt
I enjoyed a lot of explanation of flutter
I would like you to help me think
I started creating an app to create merchant accounts
I will use an internal database
I would collect a column and use the sum again
Do I use sqflite with flutter
Or use a package from no sql database packages
hi, what kind of database do you use for your projects? hive? moor? etc? any recommendations?
i got the problem for my error
void main() async{
WidgetsFlutterBinding.ensureInitialized();
var appDocumentDir = await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocumentDir.path);
runApp(MyApp());
}
add this WidgetsFlutterBinding.ensureInitialized();
before you get the path
Great content.. Thanks!
One suggestion, @13:19 wouldn't returning "Center > CircularProgressIndicator" be better UX? than showing blank white screen
Absolutely! There goes my UX prowess, I guess 😅
Basic question: What would be the difference if instead of using:
> final contact = contactsBox.get(index) as Contact;
I used this:
> Contact contact = contactsBox.get(index);
epic, this is epic.
The build runner failed until I modified the @HiveType annotation to read @HiveType(typeId:0). I am using build_runner version ^1.11.1. I am really enjoying this process. Thanks.
what vscode extension are you using to get tree-like parent-child , looks beautiful
Hey bro, hydrated_bloc or hive for ecommerce shopping cart for unregistred users? Thank you
Hello. I have a problem to find way how to make Flutter app which uses pre-populated database with some tables. Do you have any tutorial for that?
lol I was just thinkin f this, Reso might upload today or tommorrow about Hive...
I hope I didn't disappoint 😄
Great walk through of Hive, but outdated. Lots of updates in current version 2.0.4 not covered in your tutorial. Would be great to see an updated version. E.g. typeId is required to generate type adapter now and when registering an adapter, typeId cannot be passed as shown in video. I'm sure it's a pain to stay current with updates on packages. Problem is you make tutorials that are too good so we expect you'll keep them current with every minor version update of any package. :)
You are boss bro
Hi @Reso Coder,
What line identation guide extension do you use (the highlighted lines of method/ properties)?
E.g: Scaffold has highlighted lines for appBar and body and their childs.
I've searched through vscode extensions but couldn't find it.
It's actually part of the flutter/dart extension. just add "dart.previewFlutterUiGuides": true, to your settings!
Hey my question is do I need to ask for permission from user something like internal storage permission etc, or I can simply use it without permission as it is and in what file format it stores your data and where 🙄🙄 please help me out
thankss
Is it possible for you to update this? I'm trying to get this to work with the new Flutter version etc but it seems to keep crashing..
Is Hive better than Sembast? Sembast has queries so I was wondering if Hive can outperform Sembast even without queries for now..
The build fails reporting that the abstract class ContactAdapter is missing implementation of members. Did I miss something?
Is it one key for one value ??
I mean lets say i want to save a user data
Means everytime new user i need new key or it will be overwrite ?
what is the vscode extension that tracing draw lines e.g between return and build?
@Reso Coder hi sir can i store my shopping app cart with hive ??
After adding items to my box and reopening the app, im getting an error:
Unhandled Exception: RangeError: Not enough bytes available.
I can't find a proper solution for this.
Anyone know's what to do?
WatchBoxBuilder got depricated! Now its used ValueListenableBuilder.
What is the search performance of hive? In case of millions of entries, is it suitable?
Please tell me list if extension used by You.
Please make more Hive Flutter Video
Which plugin you are using for brackets?
Thanks for your work, what about floor?
I'd rather go with Moor if you want SQL.
i got error when get path in void main(). wht should i do ?
WidgetsFlutterBinding.ensureInitialized();
add this at the first line in main function
How can we make sure that the application doesn't load data from firestore every time it starts ? Only fetch when some changes is done in the database.
wdym firestore lmao
Is there anyway that you can do search in db with Hive ??
there is No Where clause !!!!
I'm having the same problem here.
I'm a beginner in flutter, I'm wondering why it is still error, I already follow about what should be the remedy. If I remove the carat notation and type flutter clean, and flutter pub get or just flutter pub get, it gives me this error.
Because every version of flutter_test from sdk depends on path 1.8.0-nullsafety and hive >=0.4.1 =1.6.0 =0.4.1
only local database or we can use it on server and how?
I might be asking a dumb question. Can Hive be used as a statemanagement as well? By creating different box
Technically, yes. But provider is just as good, simple and optimized as it gets, so it's probably better solution
how i can get the same box value on other page but within same project
are there any async methods to add or get data from hive? saving stuff on ui thread will probably freeze ui when data gets larger
Every write operation is actually async. You just don't have to await them. By default the contents of a box are cached so read operations don't access the disk. If you don't want the values to be cached, you can use "Lazy Boxes" which have async get, getAt etc.
gracias papi
Where are we defining box name as contacts ?
Does it support windows? And can I back up my data and load it on another platform?
Can you give me the name of the theme u used for your vscode?
Does Hive.openbox('contacts') will not be called every time build() is called? The doc (api.flutter.dev/flutter/widgets/FutureBuilder-class.html) says: "If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted."
Compact is a deep english word, it couldve been better if you shown an animation of how it works under the hood(like showing a real world example). I really did not understand seeing the `compactStrategy` and cant image how it works under the hood. However it is a good tutorial tho. I just do not get the use of the `compactStrategy`.
when you said "AUTOINCREMENT" at 18:00 when deleted a contact will it " AUTO DECREAMENT" i mean the KEY
don't think so. That's now how Auto Increment works
when you use clean architecture the entities will get polluted with these annotations. It looks terrible and at the end entities will depend on these annotations.
where is the starter project link? please type it down I could not find it
May anybody tells me, why is it bad practice to expose offline data directly to the UI? 30:55 of the video
In Flutter can we create a copy of hive cluster as a backup 🙄
can we give the primary key in hive ???
How about angel dart ? Work's in progress? I'm really can't wait
Backend tutorials are heavily WIP, but I can assure you that they're coming!
How can I add unique data in hive box
Does anyone else have issues with the part file not being created after running the build runner? :( It is building successfully for me but I don't get a part file..
Edit: figured it out, the part name needs to match your file name, not the class name.
thank you
Matt, Is there a TypeAdapter solution for Android Studio? (i see pubspec assist is VS Code plugin) - Manual create TypeAdapter?
Found it... Change #Lock file by Adding to pubspec.yaml dependency as any other package #analyzer: 0.38.2
TypeGen still results this>[SEVERE] hive_generator:hive_generator on lib/models/contacts.dart:
Error running TypeAdapterGenerator
You have to provide a non-null typeId.
it has been 2 years you should update codes!
Getting issues with typeId, had to add typeId to the generator and remove from registerAdapter, eg: Hive.registerAdapter(ContactAdapter()); @HiveType(typeId: 0) using hive: ^1.3.0, hive_flutter^0.3.0+1, hive_generator: ^0.7.0
Thanks, ran into that as well :)!
I am not sure why I can't add a typeId:0 to registerAdapter()....
How I implements this in TDD?
Is hive able to handle millions of items, is there a limit?
Is there any possibility to see the data stored in the boxes?
anyone?
Firebase set do hive ?
What linux distro is that?
Just in case someone else is stuck at the same point as me where he talks about the WatchBoxBuilder. Down below in the comments is the solution which I modified a little:
Widget _buildListView() {
final contactsBox = Hive.box('contacts');
return ValueListenableBuilder(
valueListenable: Hive.box('contacts').listenable(),
builder: (context, contactsBox, _) {
return ListView.builder(
itemCount: contactsBox.length,
itemBuilder: (context, index) {
final contact = contactsBox.getAt(index) as Contact;
return ListTile(
title: Text(contact.name),
subtitle: Text(contact.age.toString()),
);
},
);
},
);
}
}
Your content saved my day.... ❤️❤️❤️❤️ Great content...... Keep posting bro you are amazing.....
I m getting 'type 'String' is not a subtype of type 'Contact' in type cast' error on this line 'final contact = contactsBox.get(index) as Contact;' Thank you
Did you ever manage to resolve this? I have the same issue and literally can not find a solution.
@@JeremyWestAU sorry cant recall it nw
What's next for Hive? :(
What path we should give for flutter web?
i guess web doesnt require a path
For those who get this error for build_runner:
[INFO] Generating build script...
[INFO] Generating build script completed, took 613ms
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 25.4s
[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 1.3s
[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 3ms
[INFO] Running build...
[INFO] 1.1s elapsed, 9/10 actions completed.
[INFO] 4.3s elapsed, 9/10 actions completed.
[SEVERE] hive_generator:hive_generator on lib/modals/contact.dart:
You have to provide a non-null typeId.
Sadly It seems hive isn't maintained anymore. Also, I couldn't install the dependency for incompatibility with Flutter SDK. The only way it worked was by removing flutter_test from dev_dependencies.
I had the same problem. It helps to use dependency_overrides: dartx: ^0.4.2 ... but honestly I'm having trouble getting any Hive example thing to run properly. Overriding stuff does not make me too happy. Thinking of just trying Moor now..
@@wickedgummybear3104
in pubspec.yaml add:
hive_flutter:
git:
url: github.com/hivedb/hive.git
path: hive_flutter
Because this video is a bit old and Hive was modified I had to follow the docs to see what changed
docs.hivedb.dev
PS: don't use Hive.initFlutter()
but do use: import 'package:hive_flutter/hive_flutter.dart';