flutter 插件调用callback函数
dart plugin
class TestLib {
static MethodChannel _channel = const MethodChannel('test_lib')
..setMethodCallHandler(_methodCallHandler);
static Function _cb;
static Future<void> _methodCallHandler(MethodCall call) async {
printf("[%s], args: %o", call.method, call.arguments);
switch (call.method) {
case 'callListener':
if(_cb != null) _cb(call.arguments as String);
break;
default:
print('not method.');
}
}
static void platformVersion(Function cb) {
_cb = cb;
_channel.invokeMethod('getPlatformVersion');
}
}
kt
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
// result.success("Android ${android.os.Build.VERSION.RELEASE}")
channel.invokeMethod("callListener", "Ajanuw");
} else {
result.notImplemented()
}
}
use
TestLib.platformVersion((String name) {
print('hello $name');
})