Flutter:未处理的异常:MissingPluginException(未在通道上找到方法的实现)

时间:2020-01-01 14:39:57

标签: android kotlin flutter

我想在我的flutter代码中调用kotlin函数(只是为了使用同意sdk设置参数),但是,每次我尝试调用该函数时(目前只是测试代码),都会收到错误消息: Unhandled Exception: MissingPluginException(No implementation found for method getConsent on channel consent.sdk/consent)

这是我的MainActivity.kt的样子:

import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
import android.os.Bundle
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
    MethodChannel(flutterView, "consent.sdk/consent").setMethodCallHandler {
      // Note: this method is invoked on the main thread.
      call, result ->
      if (call.method == "getConsent") {
        result.success("Yay")
      } else {
        result.success("Yey")
        //result.notImplemented() Commented out to see if this caused the error, but it didn't
      }
    }
  }
}

这就是我试图在代码中“运行”此代码的方法:

const platform = const MethodChannel('consent.sdk/consent');
Future<String> _getConsent() async {
  String result;
  try {
    result = await platform.invokeMethod('getConsent');
  } on PlatformException catch (e) {
    result = "Failed: '${e.message}'.";
  }
  return result;
}

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

因此,我的问题通过运行得以解决: flutter clean,然后运行应用程序

感谢dlohani