how to use call dart from rust with my argument? #1931
-
I am reading the doc and I notice frb v2 supports call dart codes from rust side https://cjycode.com/flutter_rust_bridge/guides/direction/rust-call-dart however it make me confuse rustFunction(dartCallback: (name) => 'Hello, $name!'); that may mean set the callback in dart codes, so rust side can call it but in rust side pub fn rust_function(dart_callback: Fn(String) -> String) {
dart_callback("Tom"); // Will get `Hello, Tom!`
} well I don't know how to use this function // other codes
rust_function(???where can I find the closure???) or should I use something generated by frb? and if I want to pass my let name = "test".to_string();
// how to pass the variable as argument to function??? |
Beta Was this translation helpful? Give feedback.
Answered by
Sherlock-Holo
May 10, 2024
Replies: 1 comment 1 reply
-
Hi, I am not very sure about the question. Could you please elaborate a bit more? To be more explicitly, in case it helps: pub fn rust_function(dart_callback: Fn(String) -> String) {
let name = "test".to_string();
dart_callback(name); // <-- answer your last question
} void thisIsDartFunction(String name) { print('hi $name'); } // this is a dart function
rustFunction(dartCallback: thisIsDartFunction); // you can pass the dart function as an argument to Rust |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh I misunderstood the example
I thought
meant dart provide a ffi function directly, like
but actually: rust provide a function that allow dart call rust function to pass a callback to rust side, then rust can store the callback, and invoke the callback later to achieve the purpose of calling the dart codes