在FlutterPlugin中永远不会调用onAttachedToEngine的重写方法 [英] Override method onAttachedToEngine never gets called in FlutterPlugin

查看:872
本文介绍了在FlutterPlugin中永远不会调用onAttachedToEngine的重写方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现flutter插件,但是以某种方式从未调用覆盖函数onAttachedToEngine.根据此 doc

I'm trying to implement a flutter plugin but somehow the override function onAttachedToEngine never gets called. According to this doc

在调用PluginRegistry.add(FlutterPlugin)时, FlutterEngine将在以下位置调用onAttachedToEngine(FlutterPluginBinding) 给定的FlutterPlugin.

During the call to PluginRegistry.add(FlutterPlugin), the FlutterEngine will invoke onAttachedToEngine(FlutterPluginBinding) on the given FlutterPlugin.

,但从不调用它.我注意到的一件事是,它实际上是在您首次构建和调试应用程序时调用的,并在此后的任何重新构建后停止. 这是我的代码:

but it is never invoked. One thing I have noticed though is that it actually invokes the first time you build and debug the app, and ceases after any rebuilds thereafter. Here is my code:

public class Plugin : FlutterPlugin, MethodCallHandler { 

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
  channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_plugin")
  channel.setMethodCallHandler(this)
  println("Engine is attached!")
}

companion object {
  @JvmStatic
  fun registerWith(registrar: Registrar) {
    val channel = MethodChannel(registrar.messenger(), "flutter_blockstack")
    channel.setMethodCallHandler(FlutterBlockstackPlugin())
    println("Registered!")
  }
}


override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
    result.success("Android ${android.os.Build.VERSION.RELEASE}")
  } else {
    result.notImplemented()
  }
}

}

推荐答案

这是预期的行为.

在连接到FlutterEngine之后,将立即调用此函数以设置新的MethodCallHandler.在onAttachedToEngine(AndroidX)或registerWith(非AndroidX)中​​完成.

Right after attaching to the FlutterEngine, this function gets called to set up a new MethodCallHandler. This is done either in onAttachedToEngine (AndroidX) or in registerWith (non AndroidX).

实际上多次调用的函数是onMethodCall,可以随时随地用invokeMethod调用它.

The function that actually gets called more than once is the onMethodCall, that gets called anytime you want from flutter with invokeMethod.

热重启或热重新加载不会触发onAttachedToEngine,实际上,您尚未从引擎上卸下,因此您需要脱开并重新安装它才能被触发.

A hot restart or a hot reload will not trigger onAttachedToEngine, as actually, you haven't detached from the engine, so you need to deatach and reatach for it to be triggered.

我希望这种澄清可以对您有所帮助!

I hope this clarification can help you!

这篇关于在FlutterPlugin中永远不会调用onAttachedToEngine的重写方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆