FlutterPluginBinding.flutterEngine 已弃用
FlutterPluginBinding.flutterEngine is deprecated
我开发了一个Flutter插件,问题是FlutterPluginBinding.flutterEngine
得到FlutterEngine
is deprecated。完整代码如下:
class AMPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.ActivityResultListener {
private lateinit var channel: MethodChannel
private var activity: Activity? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val engine = flutterPluginBinding.flutterEngine
channel = MethodChannel(engine.dartExecutor, "aM")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
else -> result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
return true
}
override fun onDetachedFromActivity() {
activity = null
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onDetachedFromActivityForConfigChanges() {
activity = null
}
companion object {
@Suppress("UNUSED")
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "aM")
channel.setMethodCallHandler(AMPlugin())
}
}
}
但我无法理解如何以其他方式获取 FlutterEngine
或初始化 MethodChannel
。我试过 val engine = FlutterEngine(binding.applicationContext)
但它导致崩溃,似乎它需要 Activity
上下文。
如何替换这个已弃用的 API?
替换:
channel = MethodChannel(engine.dartExecutor, "aM")
和
channel = MethodChannel(flutterPluginBinding.getBinaryMessenger(), "aM")
我开发了一个Flutter插件,问题是FlutterPluginBinding.flutterEngine
得到FlutterEngine
is deprecated。完整代码如下:
class AMPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.ActivityResultListener {
private lateinit var channel: MethodChannel
private var activity: Activity? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val engine = flutterPluginBinding.flutterEngine
channel = MethodChannel(engine.dartExecutor, "aM")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
else -> result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
return true
}
override fun onDetachedFromActivity() {
activity = null
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onDetachedFromActivityForConfigChanges() {
activity = null
}
companion object {
@Suppress("UNUSED")
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "aM")
channel.setMethodCallHandler(AMPlugin())
}
}
}
但我无法理解如何以其他方式获取 FlutterEngine
或初始化 MethodChannel
。我试过 val engine = FlutterEngine(binding.applicationContext)
但它导致崩溃,似乎它需要 Activity
上下文。
如何替换这个已弃用的 API?
替换:
channel = MethodChannel(engine.dartExecutor, "aM")
和
channel = MethodChannel(flutterPluginBinding.getBinaryMessenger(), "aM")