小编Cha*_*Lee的帖子

我在使用 flutter 时遇到了 GeneratedPluginRegistrant.registerWith 方法和 MethodChannel 对象的问题

我在这里尝试示例https://flutter.dev/docs/development/platform-integration/platform-channels#step-1-create-a-new-app-project

但如果我输入完全相同的代码,它就不起作用。(我正在使用 Kotlin)

当我尝试

GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL)
Run Code Online (Sandbox Code Playgroud)

错误说

类型不匹配:推断的类型是 MainActivity 但 FlutterEngine 是预期的

未解决的参考:flutterView

所以我试过下面

GeneratedPluginRegistrant.registerWith(FlutterEngine(this))
MethodChannel(FlutterView(this), CHANNEL)
Run Code Online (Sandbox Code Playgroud)

它使错误消失,但出现另一个问题

[错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:MissingPluginException(在通道 samples.flutter.io/battery 上找不到方法 getBatteryLevel 的实现)

虽然存在 getBatteryLevel() 方法

    private fun getBatteryLevel(): Int {
        val batteryLevel: Int
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP){
            val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
            batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        } else  {
            val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
            batteryLevel = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        }
        return batteryLevel
    }
Run Code Online (Sandbox Code Playgroud)

我也试过 getFlutterView(),没有用。

这是我想知道的要点。

  1. GeneratedPluginRegistrant 和 MethodChannel 的错误解决了吗?我的意思是,这是正确的修复方法吗?如果没有,输入什么作为参数?
  2. 我不知道在出现 MissingPluginException …

android native platform-specific kotlin flutter

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

android ×1

flutter ×1

kotlin ×1

native ×1

platform-specific ×1