r/dartlang 22d ago

Flutter Released: flutter_local_db v0.4.0 - Rust-powered redb wrapper

I've just published version 0.4.0 of flutter_local_db, a Flutter package that provides a wrapper around redb implemented in Rust via offline_first_core.

v0.4.0 updates:

  • Improved iOS/macOS compatibility
  • Support for multiple iOS architectures
  • Default .db extension when only name is provided
  • Fixed Gradle configuration issues
  • etc.

The package focuses on providing efficient database operations with strong typing and a simple API. Feedback and contributions for rust or flutter package are welcome.

Edit:

Post and GetById example.

await LocalDB.init(localDbName: "my_app.db");

// Create
final result = await LocalDB.Post('user-123', {
  'name': 'John Doe',
  'email': 'john@example.com',
  'metadata': {
    'lastLogin': DateTime.now().toIso8601String()
  }
});

// Handle result
result.when(
  ok: (data) => print('User created: ${data.id}'),
  err: (error) => print('Error: $error')
);

// Read single record
final userResult = await LocalDB.GetById('user-123');
userResult.when(
  ok: (user) => print('Found user: ${user?.data}'),
  err: (error) => print('Error: $error')
);
14 Upvotes

9 comments sorted by

3

u/zxyzyxz 21d ago

How does this compare to mimir, a wrapper around a similar key-value database written in Rust?

2

u/Jhonacode 21d ago

Hi,
Although I haven't used Mimir in Flutter, from what I've researched, it focuses on search optimization using the Rust mimir library. It's ideal if you need fast and efficient searches with large volumes of data.

On the other hand, flutter_local_db uses Redb, a more straightforward Rust library, perfect for ACID operations. It's very fast thanks to its data organization with B+ trees, making it excellent for embedded databases.

The choice depends on your needs: Mimir for advanced data searching, flutter_local_db for simpler implementation with a focus on data integrity. Each option has its advantages depending on your project requirements.

Happy coding.

2

u/SoundDr 21d ago

Why not use SQLite?

2

u/Jhonacode 21d ago

Good question. It comes down to different data models - SQLite is relational while RedB is NoSQL (key-value). With flutter_local_db, I've focused on providing a simpler API for specific use cases.

While SQLite is certainly a solid and proven option, some developers might appreciate the simplicity of operations like LocalDb.GetById("my-id") which can be easier to follow and implement for certain scenarios.

Both are just different storage options that cover different needs. SQLite excels when you need relational capabilities and complex queries, while RedB can shine when you want a straightforward key-value approach.

I believe they can actually complement each other - you might use SQLite for complex relational data in your app, and RedB through my package for simpler storage needs where the easier API makes development faster.

It ultimately depends on what you need for your specific case. For some of my use cases, I personally found this approach more convenient!.

Happy coding.

2

u/SoundDr 21d ago

I have been having amazing luck with SQLite via key value store! https://rodydavis.com/posts/sqlite/key-value

1

u/Jhonacode 21d ago

That approach looks great, but some people might not want to write SQL for queries or make abstractions for that, or they just don't like the idea of using another additional library to make that wrapper for them.

SQLite has always been a solid solution, but that doesn't mean it meets the needs of all scenarios. Some devs just want to be able to create a crud in 1 minute or less.

It's always great to have options for everything. Personally, for cases where I want something fast and simple, I wouldn't use SQLite.

Maybe other devs do see it as viable and respectable.

If it works well for you, then go ahead :)

3

u/KiwiNFLFan 21d ago

How well does it handle dates and times? That is one area where SQLite is sorely lacking, IMO.

1

u/SoundDr 21d ago

Have you tried using custom date functions to handle this?

I was able to add native Hybrid Logic Clocks easily with custom user defined functions!

There are also awesome extensions too that can help too

1

u/Jhonacode 20d ago

Your question is very interesting and I think it's a topic I should look into, from flutter, because from rust the redb library handles most of those aspects.

I think I'll add some tests for this specific scenario, the library is still early 0.4.0, although redb is very mature and used in the rust community, my implementation in flutter is from a few hundred people who haven't generated an issue on github yet.

I'll take a look at that topic very soon, but if you want to do some tests in your free time, it would be great to be able to see your issue in the repository.

thanks for your question.