Assuming that you are using Swift in your plugin (called XXX), you will have a SwiftXXXPlugin
class, with a static register
method. Move channel
to become a static, rather than a local variable of register
. Then create some static methods for your iOS to Dart methods and call invokeMethod
like this:
channel.invokeMethod("someMethod", arguments: "someValue")
arguments
is Any
and could be anything that the channel knows how to encode, for example, byte array, String, int, double, etc. It can also encode lists and maps of the basic objects.
At the Dart end, you have XXX.dart. Add a static method called, for example, setHandler
, and implement the implementations of the methods. You will need to call setHandler
once before using the channel.
static setHandler() {
_channel.setMethodCallHandler(methodCallHandler);
}
static Future<dynamic> methodCallHandler(MethodCall methodCall) async {
switch (methodCall.method) {
case 'someMethod':
print(methodCall.arguments); // prints the argument - "someValue"
return null; // could return a value here
default:
throw PlatformException(code: 'notimpl', message: 'not implemented');
}
}