我想在我的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;
}
我在做什么错了?