Learn about the principles of Flutter, Golang, Python, and Chrome.
Based on deep customization and optimization with the Skia rendering engine, Flutter provides efficient rendering support and ensures absolute cross-platform rendering consistency. In addition to the UI interface, a complete App also needs some low-level capabilities of the native platform, such as persistent data storage, message push, hardware support, etc. Because Flutter takes over the rendering layer, the underlying capabilities of the system cannot be supported by the Flutter framework. Many of the relatively mature libraries currently available in Flutter native systems are not yet implemented in Flutter.
To support the underlying capabilities of native systems and invoke native platform code, Flutter provides Method channels at the logical layer. Based on the method channel, we can expose the underlying capabilities of native systems to the Dart layer as interfaces, so that the Dart uses native underlying capabilities and calls native platform code as if they were calling the Dart API.
Methods the channel
Flutter provides a flexible and lightweight mechanism to communicate between Dart and native code in order to access the underlying native code. This is the message passing mechanism for method calls. The channel used to transmit messages is the method channel.
A typical method invocation process is similar to a network request. The client of the Flutter sends a method request through the method channel to the native code host as the server. After listening for the request, the native code host calls platform-specific methods to process the Flutter request. Finally, the processing results are sent back to the Flutter layer through the method channel.
As can be seen from the figure above, the processing and response of method calls are registered in FlutterView on Android platform and FlutterViewController on iOS platform. FlutterView and FlutterViewController provide the Flutter application with a sketchpad on which the Flutter can draw the desired visual effect via the Skia engine.
FlutterView and FlutterViewController are not only the container for a Flutter application, but also the entry point for a Flutter application, which is the most appropriate place to register method calls.
The following uses App jumping to the App market as an example to demonstrate how to use the method channel.
First, we need to provide a unique string as a unique identifier to construct a method channel, and then specify a call to the custom method “openAppMarket” on this method channel to initiate a request to open the app Store:
[Note] Just like network requests, the invocation of the method channel will also have exceptions, so it is necessary to deal with exceptions to avoid App crash caused by abnormal invocation of the method channel.
The code above implements the invocation of a custom method channel at the Dart level, which then needs to respond to the channel method on Android/iOS.
Android interface implementation
On Android, method channels are registered in the entry to Flutter, which is MainActivity’s FlutterView. Therefore, we need to find the Android host of Flutter application, mainActivity.java, Add the method channel registration and custom method implementation in MainActivity’s onCreate method.
Register the method channel in MainActivity’s onCreate method:
Interface implementation on iOS
Like the Android platform, the approach channel of registration is applied in Flutter entrance, we need to found in the Flutter application of iOS App host AppDelegate. M file, registered in didFinishLaunchingWithOptions method approach channel.
In didFinishLaunchingWithOptions method to add the channel’s statement:
This should complete the implementation of the method channels and interfaces in the Android and iOS platforms. Then the native code can be invoked with the method channels in the Flutter layer.
Note that we pass a result back to the Dart layer after processing the request in the native code layer. Dart, Android, and iOS use three different data types for the same callback parameter: java.lang.Integer for Android, NSNumber for iOS, and int for Dart.
Dart addresses cross-system data interaction by using StandardMessageCodec to serialize jSON-like binary data transferred over the channel to communicate with standardized data. When different systems send/receive transfer parameters, they serialize/deserialize the transfer parameters according to the characteristics of the current system.
The example above, the native platform. The Java lang. Integer/NSNumber type back parameters for serialization, first converted to binary data, then by method of channel transmission, such as the data is passed to the Dart layer, the system will binary data into a data type int.
The mapping between different types of data on Android, iOS, and Dart is as follows: