Closed anwarsalim08 closed 6 months ago
Hi! Thanks for opening your first issue here! :smile:
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,
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!');
@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.
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.
@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
@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.
@fzyzcjy Great!
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
I am getting the "Application Support Dir" path from Flutter.
here I store the app state as a binary via Serde, completely from rust.
Flutter is the UI layer only. Keep in mind that any user input/files need to go through Dart to Rust. This is a trivial method call, but not zero-cost, as FFI requires copying the content. The opposite direction (Rust -> Flutter) is zero-cost (just a pointer reference).
I follow an event-driven approach, inspired by crux: -- Any action in Flutter can execute an Event, which is a enum in Rust, processed by a "processEvent" function call. -- This call returns a List of Effects, which are instructions to Flutter what to do. Implemented on the Flutter side. For example, the return Effect could be "render", which can include a viewModel. Fuller would implement presenting this data.
I don't plan to have any state management in Flutter. I don't think setState needs to be called by Rust. I am doing it indirectly: The returned Effect is triggering a setState call on the Flutter side. So, maybe this can be avoided, maybe it should be done more direct ... I set that up with FRB v1 (it is updated to FRB v2 now). I tried to convert it to stateless widgets, but I had troubles to call the rust-future more than the initial time.
Maybe this helps - and, @anwarsalim08, I am interesting to learn about your approach!
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?
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 :)
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.
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.
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.