fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.23k stars 291 forks source link

Data Stoarge Example #1691

Closed anwarsalim08 closed 6 months ago

anwarsalim08 commented 9 months ago

I need guidance on using Rust for local storage in mobile apps, specifically for iOS and Android. I want to store app state, tokens, and similar data.

I'm considering a file-based approach like SQLite, but I'm unsure how to manage the connection paths across different platforms.

How can I achieve this?

Another approach i thought of is letting the storage to be handled by flutter and invoking the fetching and adding data from rust.

But again i didn't understand the docs of invoking dart function from rust.

welcome[bot] commented 9 months ago

Hi! Thanks for opening your first issue here! :smile:

fzyzcjy commented 9 months ago

Hi, just write arbitrary Rust code - flutter_rust_bridge does not limit the ways of doing storage. There are many different ways, and here are some quick suggestions:

I'm considering a file-based approach like SQLite, but I'm unsure how to manage the connection paths across different platforms.

One way may be,

  1. https://pub.dev/documentation/path_provider/latest/path_provider/getApplicationSupportDirectory.html in dart to get a path you like
  2. pass path to Rust as an argument, and in rust you play with sqlite etc

But again i didn't understand the docs of invoking dart function from rust.

https://cjycode.com/flutter_rust_bridge/guides/direction/rust-call-dart

I guess maybe you need some background about Rust's async and impl and closures, because the example is using standard Rust. (No worries - everyone is not a Rust expert when born!)

// Rust
pub async fn rust_function(dart_callback: impl Fn(String) -> DartFnFuture<String>) {
    dart_callback("Tom".to_owned()).await; // Will get `Hello, Tom!`
}

// Dart
await rustFunction(dartCallback: (name) => 'Hello, $name!');
guchengxi1994 commented 9 months ago

@anwarsalim08 if you want to use storage in flutter , I think using isar https://github.com/isar/isar is a good option. Besides, isar is also built on rust. If you want to implement storage on your own with rust, you can try sqlx https://github.com/launchbadge/sqlx . If you just want to storage some k-v pairs, I think shared-preference is better.

anwarsalim08 commented 9 months ago

Hello both,

The main idea i am going through this route is that i want the entire business logic to be on the rust side, including the storing and retrieving app state. The main issue i had is that i didn't know how to get the path which @fzyzcjy illustrated.

Therefore, i will attempt to use https://github.com/vincent-herlemont/native_db and see how this goes.

guchengxi1994 commented 9 months ago

@anwarsalim08 emmm.... as far as I know, only static function from dart side can be called in c++. I am not sure if flutter_rust_bridge can call functions like setState or something else (I still use version 1.82 in my project). If it could, that is awsome

fzyzcjy commented 9 months ago

@guchengxi1994 as far as I know, only static function from dart side can be called in c++. I am not sure if flutter_rust_bridge can call functions like setState or something else (I still use version 1.82 in my project). If it could, that is awsome

You can call everything in v2 :) https://cjycode.com/flutter_rust_bridge/guides/direction/rust-call-dart The doc shows how to call arbitrary Dart function from Rust function, using the syntax of Rust's (standard) callables.

guchengxi1994 commented 9 months ago

@fzyzcjy Great!

patmuk commented 8 months ago

I am a bit late to the party ... @anwarsalim08 I hope you found a way to implement it (if you have a public repo showcasing this I would be interested).

I not: I try the same thing and made an example application for that: https://github.com/patmuk/flutter-rust-bridge_crux_style

Maybe this helps - and, @anwarsalim08, I am interesting to learn about your approach!

fzyzcjy commented 8 months ago

FFI requires copying the content

If you use rust auto opaque, then the states are always in rust side and does not require copying. But yes, if something was in dart and you want to transfer it to rust, it needs copying.

I had troubles to call the rust-future more than the initial time

Not sure, does #[frb(sync)] which gives you sync methods help?

patmuk commented 8 months ago

I had troubles to call the rust-future more than the initial time

Not sure, does #[frb(sync)] which gives you sync methods help?

It is awesome how responsive you are!

Sorry for being unclear. I just recall that partially, but it was not about frb at all, more about when Flutter decides to repaint. My idea was to have stateless widgets only, and to keep the state on the rust side. However, a button press, for example, triggered updating the state, but the widget would not rebuild and call a view() function from rust to see the update. This is simplified, but I realized that stateless widgets are quite static, and something up the tree needs to be statefull, so that a repaint can be triggered. my view() function is async - but I don’t think making it sync would help.

Nevertheless, it is just cosmetic. I do have statefull widgets, and I call setState() to trigger a repaint - but the state is not with Flutter :)

stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 5 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.