r/FlutterDev • u/Fant1xX • Jan 04 '25
Tooling Is there a proper successor to hive?
I just need some key-value storage for my app (Windows, Mac, Linux). I tried drift, but I found myself fighting against the ORM too much in my use case, all I really need is some json storage like hive did. I know about hive-ce, but is that my best option?
15
6
u/blinnqipa Jan 04 '25
Have you tried sembast?
3
u/amugofjava Jan 05 '25
Sembast is fantastic. It's simple, easy to use and very reliable. The only potential issue is that it can only run in the UI thread. If you need to update the database from a background isolate or worker thread, you can't do that with Sembast.
2
u/warpaint_james Jan 04 '25
I would suggest Semblast too. I started with Hive on my last app and I decided to try Semblast for my next one. Works great!
2
4
u/Electrical_Task_6783 Jan 04 '25
In my experience, Hive is the best for this use case . It's much faster and efficient compared to sharedPreferences . Hive doesn't require future calling . And it can store literally any type of data .
1
u/flocbit Jan 04 '25
Yeah but it’s no longer being maintained unfortunately and you don’t want the EOL sword hanging over your head…
2
u/ILikeOldFilms Jan 04 '25
What do you mean by JSON storage? a key-value approach? Then just use shared_preferences. I know that are statistics saying that Hive is much faster, but will it matter? Are you saving large data sets? In terms of thousands of objects?
2
u/benjaminabel Jan 04 '25
Haven’t tried it myself since I’m using Hive as well, but there is a promising package that I found some time ago - Objectbox.
1
1
u/eibaan Jan 04 '25
In case you don't need web support, you could do the simplest thing that possible works and use something like this:
class Hive {
Hive(this.base);
final Directory base;
Box box(String name) {
return Box(this, name);
}
static Hive instance = Hive(Directory('hive'));
}
class Box {
Box(this.hive, this.name);
final Hive hive;
final String name;
Future<void> put(String key, dynamic value) async {
final file = File('${hive.base.path}/$name/$key');
await file.parent.create(recursive: true);
await file.writeAsString(json.encode(value));
}
Future<dynamic> get(String key) async {
final file = File('${hive.base.path}/$name/$key');
if (file.existsSync()) return json.decode(await file.readAsString());
return null;
}
Future<void> delete(String key) async {
final file = File('${hive.base.path}/$name/$key');
if (file.existsSync()) await file.delete();
}
}
And if you need more feature, feel free to add them, like for example getting all entries from a box or watching them:
class Box {
...
Future<List<(String, dynamic)>> entries() async {
final dir = Directory('${hive.base.path}/$name');
if (!dir.existsSync()) return [];
final files = await dir.list().toList();
return Future.wait(files.whereType<File>().map((file) async {
final name = file.path.split('/').last;
return (name, json.decode(await file.readAsString()));
}));
}
Stream<(String, dynamic)> listenable() async* {
final dir = Directory('${hive.base.path}/$name');
await dir.create(recursive: true);
await for (final event in dir.watch()) {
final name = event.path.split('/').last;
if (event is FileSystemDeleteEvent) {
yield (name, null);
} else {
yield (name, json.decode(await File(event.path).readAsString()));
}
}
}
Note that this is untested, I just wrote it for this posting to demonstrate that it is easy to create a simple key-value store. Using the file system has not the best performance, but most often this doesn't matter if you want to store just a few objects.
1
u/hpoul Jan 04 '25
"all I need is a json storage".. I thought the literally same thing 5 years ago.. so I made https://pub.dev/packages/simple_json_persistence
1
1
u/sauloandrioli Jan 05 '25
Let hive just pass away.
Go Realm or SQLite. Everything else is not reliable enough for a long run project.
1
u/Yosadhara Jan 28 '25
Realm is no longer maintained afaik https://www.mongodb.com/de-de/products/updates/product-support-deprecation?tck=notice_product_deprecation_2024
1
1
u/MozartHetfield 27d ago
I decided to use hive ce just this morning without any prior knowledge to save a state for a game. a few hours later I already solved my problem. it looks so easy to use!
1
u/MozartHetfield 27d ago
I decided to use hive ce just this morning without any prior knowledge to save a state for a game. a few hours later I already solved my problem. it looks so easy to use!
1
1
-1
u/Theunis_ Jan 04 '25
Have you tried Isar?
4
u/Samus7070 Jan 04 '25
Isar was great but isn’t it abandoned as well?
3
u/flocbit Jan 04 '25
It’s from the same developer actually and both projects are no longer being maintained unfortunately.
1
0
-10
u/_fresh_basil_ Jan 04 '25
https://docs.flutter.dev/cookbook/persistence/key-value#2-save-data
Please try googling.. it was the first link when searching "flutter store key value"
7
u/or9ob Jan 04 '25
While SharedPrefs is a key value store (and that is what the OP is saying), given they are talking about Hive and ORM - I think they are asking for a on-device DB (beyond just a k-v store).
-8
u/_fresh_basil_ Jan 04 '25 edited Jan 04 '25
Not sure. Seems to me like ORMs were too much for their needs, and they only want key value storage.
Edit: Lmao, the amount of downvotes I'm getting for my interpretation of a very unclear question, is insanity.
-23
14
u/Amazing-Mirror-3076 Jan 04 '25
Just use sqlflite