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.1k stars 278 forks source link

Key type #2196

Closed Ggcpp closed 1 month ago

Ggcpp commented 1 month ago

I'm not sure if it's the right place to ask it but I'm trying to store some widget's keys in Rust but I don't know what type I should use in my Rust code to store Flutter's Key type. I searched in the doc but I can't find a concrete way to do so. So is it possible? And if not maybe it could be interesting to add a Key type for Rust to use?

welcome[bot] commented 1 month ago

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

Ggcpp commented 1 month ago

Nevermind I can just store the keys as a Strings with a key.toString() on the dart side :') It's too early in the morning my brain doesn't work

fzyzcjy commented 1 month ago

Nevermind I can just store the keys as a Strings with a key.toString() on the dart side

I think so, that looks like one possible way (especially assuming e.g. you are using ValueKey<String>)

Ferry-200 commented 1 month ago

I am interested in why storing widget's key in rust.🧐

Ggcpp commented 1 month ago

I am interested in why storing widget's key in rust.🧐

My approach might not be ideal, but my goal was to use specific widget methods as callbacks in Rust. When a widget is created, I register one of its methods as a callback in Rust, along with a unique key for that widget. Then, when the widget is recreated or destroyed, I unregister its callback in Rust within the destructor. Since there are multiple widgets with multiple registered callbacks, I need a way to unregister only the callbacks associated with the destroyed widget, which is why I store their keys in Rust.

And why register some widget methods as callbacks in Rust? Because I need a way to adjust the widget states based on events that occur on the Rust side.

Let me know if my explanation is not clear or if you want more details by the way sorry for my unnatural english ;-;

Ferry-200 commented 1 month ago

I agree with the first paragraph. But if a widget's state depends on rust's event, passing a enum which express the event to dart side is a better approach in my opinion.

pub enum LoadingState {
    Loading, 
    Done
}

pub fn load(sink: StreamSink<LoadingState>) {
    sink.add(LoadingState::Loading);
    thread::sleep(Duration::from_secs(1));
    sink.add(LoadingState::Done);
}
StreamBuilder(
    stream: load(),
    builder: (context, snapshot) {
        return switch(snapshot.data) {
            LoadingState.loading => Text("loading"),
            LoadingState.done => Text("done"),
        };
    }
)

Let me konw if I have any misunderstanding on your situation.

Ggcpp commented 1 month ago

Oh well I didn't know about StreamBuilder widgets, I'm very new to Dart and Flutter. However I'm not sure how to implement this anyway. For now my situation is this: I have a local server running in Rust that needs to update some widgets in Flutter when it receives external data for example. When an event from the server occurs, the only way to inform Flutter that something happened is by calling a callback (maybe I'm wrong here?). From here, how can I call the stream function to update a state in Flutter? And if I update the state directly in Rust, how will Flutter know that a state has just changed? I feel like I still need to register the StreamBuilder stream as a callback in Rust but maybe I'm missing something obvious?

Let me know if it's not clear!

fzyzcjy commented 1 month ago

Close since this seems to be solved, but feel free to discuss further, or reopen if needed!

github-actions[bot] commented 2 weeks 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.