Custom Platform Specific Code(Flutter)-Android Native
The following article describes how to write custom platform specific code in both native-android and flutter with basic “Hello-World” example.
Flutter uses a flexible system that helps developer to call platform specific API’s both for Kotlin and Java on Android.
- The flutter part(dart code) of the app sends the message to the android host over a platform channel().
- The android host(native) listens on the platform channel, and receives the message sent by client(flutter part[dart]) and calls into API’s using Kotlin (or) Java and sends back the message to the client(flutter part[dart]).
Below, you can see the basic architectural diagram for reference…!!!
Note:- Messages are passed asynchronously to ensure responsive user interface.
Example: Basic “Hello-World” program to understand how message are passed between the UI(flutter) and the Platform(native android).
Native(Kotlin)-MainActivity.kt
import io.flutter.embedding.android.FlutterActivity
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: io.flutter.embedding.android.FlutterActivity(){
private val CHANNEL = “flutter.native/helper”//Name as per your convenience…!!!
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
if (call.method == “helloFromNativeCode”) {
val greetings = helloFromNativeCode()
result.success(greetings)
}
}
}
private fun helloFromNativeCode(): String {
return “Hello World”
}
}
Client(dart)-main.dart
//Message to be displayed in UI…!!!
String _responseFromNativeCode = ‘Waiting for Response…’;//Initial response.
//function to be called to pass the message to platform…!!!
static const platform = const MethodChannel(‘flutter.native/helper’);
Future<void> responseFromNativeCode() async {
String response = “”;
try {
final String result = await platform.invokeMethod(‘helloFromNativeCode’);
response = result;
} on PlatformException catch (e) {
response = “Failed to Invoke: ‘${e.message}’.”;
}
setState(() {
_responseFromNativeCode = response;
});
}
Replace build with small user interface that displays the passing message…!!!
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
child: Text(‘Get Response from Native…!!!’),
onPressed: responseFromNativeCode,
),
Text(_responseFromNativeCode),
],
),
),
);
}
Do test the above example provided before proceeding further in the journey of writing platform specific code for native android in flutter…!!!
In the end have a look at the standard platform channels which uses standard message codec that supports efficient binary serialization of simple JSON-like values, such as booleans, numbers, Strings, byte buffers etc…The serialization and deserialization of these values to and from messages happens automatically when you send and receive values.
The following table shows how dart values are received on the platform side(Android) and the client side(Flutter).
Thank You…!!!