So I wrote a Flutter App about 4 Months ago. Now I wanted to do a small change, but I can't compile the app anymore, because GeneratedPluginRegistrant.registerWith(this) doesn't work anymore, I didn't change the Kotlin code only the Dart code.
The "this" in "GeneratedPluginRegistrant.registerWith(this)" shows me this error:
Type mismatch.
Required: FlutterEngine!
Found: MainActivity
The MainActivity Class:
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.view.FlutterMain
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this) // here is the error: Type mismatch. Required: FlutterEngine! Found: MainActivity
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "helloFromNativeCode") {
val greetings = helloFromNativeCode()
result.success(greetings)
}
}
}
private fun helloFromNativeCode(): String {
return "Hello from Native Android Code"
}
companion object {
private const val CHANNEL = "flutter.native/helper"
}
}
And if is use:
import io.flutter.embedding.android.FlutterActivity
instead of
import io.flutter.app.FlutterActivity
I can use
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
but have trouble with:
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "helloFromNativeCode") {
val greetings = helloFromNativeCode()
result.success(greetings)
}
}
because i get an error on flutterView:
Unresolved reference: flutterView
The code would look like this:
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.view.FlutterMain
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result -> // here is the error
if (call.method == "helloFromNativeCode") {
val greetings = helloFromNativeCode()
result.success(greetings)
}
}
}
private fun helloFromNativeCode(): String {
return "Hello from Native Android Code"
}
companion object {
private const val CHANNEL = "flutter.native/helper"
}
}
I hope someone can help me.