flutter_rust_bridge
flutter_rust_bridge copied to clipboard
EventChannel support
I want to pass data from Rust to Flutter in a stream way. That is, I don't want Flutter do call Rust to get an answer, but Rust could call and send stuff to Flutter at any time.
There's a solution for that in Java, called EventChannel, like here: https://github.com/seamusv/event_channel_sample/blob/master/android/app/src/main/java/com/yourcompany/event_channel_sample/MainActivity.java#L28
Would be nice to have them directly on Rust!
Hi! Thanks for opening your first issue here! :smile:
There are a few approaches:
- To reuse current codebase, maybe let Flutter call Rust and open a "stream", and later rust can put things into this stream and flutter will receive it. This is what I have done for my own app for "let rust send logging data to flutter".
- You may modify flutter_rust_bridge, use the fact that Rust can send data to Dart via
IntoDart
(and the underlying dart_native_api)
Feel free to make a PR!
Well I looked at EventChannel example. It is just like approach 1, the stream.
Is it possible to open a never ending stream from flutter into rust such that rust can always send things back? This would be nice because the EventChannel thing I listed requires rust to call java to call flutter, so it's platform specific and bad.
Is it possible to open a never ending stream from flutter into rust such that rust can always send things back?
Yes. For my personal usage (the logging), I do not end the stream
On the docs it says
pub struct LogEntry {
pub time_millis: i64,
pub level: i32,
pub tag: String,
pub msg: String,
}
// Simplified just for demonstration.
// To compile, you need a OnceCell, or Mutex, or RwLock
// Also see https://github.com/fzyzcjy/flutter_rust_bridge/issues/398
lazy_static! { static ref log_stream_sink: StreamSink<LogEntry>; }
pub fn create_log_stream(s: StreamSink<LogEntry>) {
stream_sink = s;
}
I think it should be
pub fn create_log_stream(s: StreamSink<LogEntry>) {
log_stream_sink = s;
}
right?
Anyways, I'm getting
the trait bound
DartCObject: From<LogEntry> is not satisfied
even after codegen.
Also I think it would be nice to mention
use flutter_rust_bridge::{StreamSink};
cau I thouoght it was a Rust thing, not a type from this lib
right?
Sure
even after codegen.
Please file a separate issue about this
Also I think it would be nice to mention
Sure, feel free to PR
Hmmm I'm using https://github.com/fzyzcjy/flutter_rust_bridge/pull/293 I think this might be the reason of the errors, cause it's old. Now I don't know what to do, cause I need it working but also need StreamSink
https://github.com/fzyzcjy/flutter_rust_bridge/pull/537
Sorry my type was not pub
so it wasn't being translated. Anyways, do you think I should have many streams per instance or just one stream and filter the messages? I don't know how expensive this stream is.
Thank you for your help!
Sorry my type was not pub so it wasn't being translated
Feel free to PR and warn (or log) such cases
Anyways, do you think I should have many streams per instance or just one stream and filter the messages? I don't know how expensive this stream is.
IMHO it is not expensive, so it should not be problem if only dozens of streams. But if you have a million stream maybe too much
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.