SoFunction
Updated on 2025-03-06

Detailed explanation of how Flutter calls Android Native

The method of Flutter calling Android Native is implemented through MethodChannel:

On the Android side:

  • Create a Class to implement FlutterPlugin and MethodCallHandler interfaces
  • Rewrite onAttachedToEngine(), onDetachedFromEngine(), onMethodCall()
  • In onAttachedToEngine(), create a MethodChannel based on the customized CHANNEL_NAME, and in onDetachedFromEngine, release MethodChannel
  • In onMethodCall, the invokeMethod communication to Native is responded to the Flutter communication with invokeMethod in Native through the customized METHOD_NAME. The code is as follows
class MethodChannelPlugin : FlutterPlugin,  {

  private var methodChannel: MethodChannel? = null
  private var mNoteForFlutterListener: NoteForFlutterListener? = null

  companion object {
    private const val CHANNEL_NAME = "method_channel"
    private const val METHOD_NAME = "saveNote"
    val instance: MethodChannelPlugin by lazy(mode = ) {
      MethodChannelPlugin()
    }
  }

  override fun onAttachedToEngine(binding: ) {
    methodChannel = MethodChannel(, CHANNEL_NAME)
    methodChannel?.setMethodCallHandler(this)
  }

  override fun onDetachedFromEngine(binding: ) {
    methodChannel?.setMethodCallHandler(null)
    methodChannel = null
  }

  override fun onMethodCall(call: MethodCall, result: ) {
    if ( == METHOD_NAME) {
      val content = <String>("content")
      mNoteForFlutterListener?.sendData(content!!)
      ("success")
    } else {
      ()
    }
  }

  fun setListener(noteForFlutterListener: NoteForFlutterListener) {
    mNoteForFlutterListener = noteForFlutterListener
  }

On the Flutter side:

  • Create MethodChannel based on METHOD_NAME defined in Native
  • By (METHOD_NAME, params), the parameter METHOD_NAME is the METHOD_NAME defined in Native, and params is the passed parameter, you can communicate with Native. In Native's onMethodCall method, it is determined by == METHOD_NAME whether Flutter calls the METHOD_NAME method defined in Native. The code is as follows
class NoteMainFulState extends State&lt;NoteMainFul&gt; {
 //flutter and native communication static const _methodMessageChannel = MethodChannel("method_channel");
 TextField textField;
 TextEditingController textEditingController;

 @override
 Widget build(BuildContext context) {
  return Scaffold(
   body: Container(
    margin: (20.0, 60.0, 20.0, 20.0),
    child: Column(
     children: [
      Container(
       child: Column(
        children: [
         textField = TextField(
          //Remove the underline          decoration: InputDecoration(border: ),
          enabled: true,
          controller: textEditingController = NoteTextEditingController(),
          keyboardType: ,
          textInputAction: ,
          maxLines: null,
         ),
         RichText(
          text: TextSpan(),
         ),
        ],
       ),
      ),
      Container(
       color: ,
       height: 50,
       child: Row(
        mainAxisAlignment: ,
        children: [
         GestureDetector(
          child: Icon(
           ,
          ),
          onTap: clickOut,
         )
        ],
       ),
      )
     ],
    ),
   ),
  );
 }

 void clickOut() async {
  //Calling the native method, return to the homepage  var content = ;
  Map&lt;String, dynamic&gt; map = {"content": content};
  var result = await _methodMessageChannel.invokeMethod("saveNote", map);
  print("result $result");
 }

The above is the process of Flutter calling Android Native methods. If you have time, analyze their implementation principles.
to be continued....
At the same time, my current Flutter version is:
Flutter 1.22.3 • channel stable • /flutter/flutter
Framework • revision 8874f21e79 (3 months ago) • 2020-10-29 14:14:35 -0700
Engine • revision a1440ca392
Tools • Dart 2.10.3

This is the end of this article about the detailed explanation of how Flutter calls Android Native. For more related content on Flutter calls Android Native, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!