Home > OS >  How to make a FlutterActivity be a MethodCallHandler?
How to make a FlutterActivity be a MethodCallHandler?

Time:06-26

I made my FlutterActivity MainActivity a MethodCallHandler:

class MainActivity: FlutterActivity(), MethodCallHandler {

so I implemented onMethodCall.

However, to be able to process method calls, still something is missing. This is how it's done in a Flutter plugin:

class MyPlugin: FlutterPlugin {
  private lateinit var channel : MethodChannel
  private val LOG_TAG: String = "MyPlugin.kt"
  
  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "myplugin")
    channel.setMethodCallHandler(this)
  }

How can I make my MainActivity be the actual MethodCallHandler?

CodePudding user response:

configureFlutterEngine provides binding between Dart and native.

class MainActivity: FlutterActivity() {

  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "<your_channel>").setMethodCallHandler {
      call, result ->
      // TODO
    }
  }
...
  • Related