I am writing a native plugin that, in some cases, has to call functions in the flutter portion of the app, written in Dart. How it's achieved, is explained here: https://flutter.io/platform-channels/
Furthermore, an example of invoking a method from the native/platform part towards the Dart/non-native is here: https://github.com/flutter/plugins/tree/master/packages/quick_actions
Now, this example is really nice in case the platform only needs to invoke a method
, i.e. that call returns nothing/void
, but in case it needs to invoke a function
, i.e. needs a return value from the non-native/Dart part, I could not have found an example or documentation on the internet. I believe it can be implemented though, because in the native Java part, there is a method:
public void invokeMethod(String method, Object arguments, MethodChannel.Result callback)
So, there is a callback
object that could have a return value from the non-native part - or, I am mistaken here, and there is currently no way of returning a value from the non-native Dart portion of the app?
The signature is
void setMethodCallHandler(Future<dynamic> handler(MethodCall call))
, so we need to provide a function at the Dart end that returnsFuture<dynamic>
, for example_channel.setMethodCallHandler(myUtilsHandler);
Then implement the handler. This one handles two methods
foo
andbar
returning respectivelyString
anddouble
.At the Java end the return value is passed to the
success
method of theResult
callback.