problem in configureFlutterEngine method writing kotlin code in flutter
Solution 1
Instead of configureFlutterEngine() method, I used onCreate() and now is working fine.
override fun onCreate(savedInstanceState: Bundle?) {
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL).setMethodCallHandler {
call, result ->
// Note: this method is invoked on the main thread.
// TODO
}
}
Instead of passing flutterEngine.dartExecutor.binaryMessenger
to MethodChannel
, pass flutterView
.
Solution 2
This is how I used it in kotlin
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val CHANNEL = "samples.flutter.dev/battery"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
// Note: this method is invoked on the main thread.
// TODO
}
}
}
Make sure to use same imports as shown. Read Docs
Alireza Abiri
Updated on December 17, 2022Comments
-
Alireza Abiri over 1 year
I wanted to write platform specific code in flutter, according to the flutter documentation we should override configureFlutterEngine method like code snippet below:
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> // Note: this method is invoked on the main thread. // TODO } }
but when I pass flutterEngine to GeneratedPluginRegistrant.registerWith(), It says:
Type mismatch: inferred type is FlutterEngine but PluginRegistry! was expected
I've already checked link below in github but didn't find any solution working for me. https://github.com/flutter/flutter/issues/45231
here is the link of flutter documentation for writing platform specific code. https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-kotlin-tab#example-project
-
Pawan almost 4 yearsHow to get FlutterView ? It also gives error at registerWith(this)
-
Omar Essam over 2 yearsWorked . . Thank you bro
-
Rashmi Bhandari about 2 years@Zohab Ali For me configureFlutterEngine it's not calling. Any idea what can be an issue. Followed the same steps as above.