• 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');
    })
    
  • 相关阅读:
    nginx负载均衡实现
    shiro 退出 清除缓存
    从零到实现Shiro中Authorization和Authentication的缓存
    Mysql 语句
    N! java
    大数java(pow)
    HDU_1548
    Mike and strings 798B
    Array Division 808D
    poj_1979(dfs)
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13843066.html
Copyright © 2020-2023  润新知