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');
})


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM