How to pass a message from Flutter to Native?

778 views
Skip to first unread message

Adam Hurwitz

unread,
Mar 4, 2018, 2:22:27 PM3/4/18
to Flutter Dev
How would you pass info from Flutter back to Android/Native code if needed to interact with a specific API / hardware component? 

Are there any Event Channels that can send info the other way or something similar to a callback?

1. The platform_channel documentation points out "method calls can also be sent in the reverse direction, with the platform acting as client to methods implemented in Dart. A concrete example of this is the quick_actions plugin." I don't see how the native side is receiving a message from Flutter in this instance.

2. It looks like a BasicMessageChannel’s send() method can be used to send "the specified message to the platform plugins on this channel". Can anyone provide a simple implementation example of this?


Mikkel Ravn

unread,
Mar 5, 2018, 3:11:38 AM3/5/18
to Adam Hurwitz, Flutter Dev
Hi Adam,

You'll find a simple example of Flutter-to-platform communication in the examples folder in the Flutter repo (Flutter sender, Android receiver, iOS receiver).

For many more examples of two-way communication between Flutter and the Android/iOS platform, take a look at our plugins repo. Most of these use MethodChannels for the communication (irrespective of direction), BasicMessageChannel being a bit too low level for common usage.


Cheers,
Mikkel

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Mikkel Nygaard Ravn
Software Engineer

Adam Hurwitz

unread,
May 12, 2018, 2:08:22 PM5/12/18
to Mikkel Ravn, Flutter Dev
Thanks for the specific code example Mikkel! 

Can you send information like a network response in the Flutter sender to an Android receiver instead of a method call?

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Mikkel Nygaard Ravn
Software Engineer

--
ASH

Mikkel Ravn

unread,
May 13, 2018, 2:41:44 PM5/13/18
to Adam Hurwitz, Flutter Dev
Not entirely sure what you are looking for. Sending information from Flutter to platform-specific code can be done use MethodChannels, by passing in a second argument after the method name:

myMethodChannel.invokeMethod('someMethod', someInformation);

All you need is to package your information using the allowed types for the channel.

On the platform side, you'd register a method call handler with the channel. When called, that handler will receive a MethodCall object whose arguments field hold the information sent from Flutter. See e.g. the url_launcher plugin for an example (Android, iOS).

Adam Hurwitz

unread,
May 15, 2018, 3:58:16 PM5/15/18
to Mikkel Ravn, Flutter Dev
I will have to dive into the sample code more in depth to test my understanding.

Thanks for the specific example! 
--
Adam LinkedIn Twitter

ged...@gmail.com

unread,
May 16, 2018, 3:49:00 PM5/16/18
to Flutter Dev
You said you wanted to connect it to a network.

What you can do is pass protobuf as a byte[] between flutter and java / swift.
Then you can use the protobuf to codegen the dart code too if you want.

You can then use that same protobuf with grpc to do network communications.

Not sure if this helps. I was using json but am trying out protobufs because it's quite a bit faster at runtime.

ged...@gmail.com

unread,
May 16, 2018, 3:50:04 PM5/16/18
to Flutter Dev

TruongSinh Tran-Nguyen

unread,
Mar 31, 2019, 6:37:47 AM3/31/19
to Flutter Dev
Hi, I happen to write an article about using Protobuf with Platform channels https://medium.com/@i_2412/flutter-platform-channels-with-protobuf-e895e533dfb7
Reply all
Reply to author
Forward
0 new messages