Currently, Flutter is a very popular cross-platform mobile UI framework. Flutter uses Dart language for making cool looking apps for android, iOS, web or desktop applications. Sometimes we need platform-specific native code to perform our task. In Flutter, the method channel is a very helpful way to run platform-specific code. You can run Java/Kotlin for android and Swift code for iOS from your dart code very easily. In this tutorial post, I'll show you how you can run the platform-specific code using the flutter method channel.
Goal: We'll run java code for getting device model, manufacturer name, os version on the android side and get the result via flutter method channel features. You can do whatever you need according to your application need.
Goto android/app/src/main/java
and open MainActivity.java
file.
import android.os.Build;
import org.json.JSONObject;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.Result;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "device";
Result pendingResult;
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
pendingResult = result;
if (call.method.equals("getDeviceInfo")) {
getDeviceInfo();
}
}
);
}
void getDeviceInfo() {
try {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
String sdkVersion = Build.VERSION.SDK;
String osVersion = Build.VERSION.RELEASE;
JSONObject obj = new JSONObject();
obj.put("platform", "android");
obj.put("model", model);
obj.put("manufacturer", manufacturer);
obj.put("os_version", osVersion);
obj.put("sdk_version", sdkVersion);
pendingResult.success(obj.toString());
} catch (Exception e) {
pendingResult.error("101", e.getMessage(), null);
}
}
}
Look at the code, here device
is our channel name (you can define it whatever name you want) and getDeviceInfo
will be our method name.
Now on the flutter side, We can run the native java code directly by the flutter method channel.
Import dart:convert
for JSON decoding and services.dart
for MethodChannel.
import 'dart:convert';
import 'package:flutter/services.dart';
const deviceChannel = const MethodChannel('device');
static Future<dynamic> getDeviceInfo() async {
try {
var result = await deviceChannel.invokeMethod('getDeviceInfo');
return json.decode(result);
} on PlatformException catch (e) {
print("Method channel error "+e.message);
}
}
@override
void initState() {
super.initState();
getDeviceInfo().then((device){
print('method channel result');
print(device["model"]);
});
}
Hope this tutorial post help you to get a real flutter method channel example. You can do a lot of things to make a powerful application with flutter using flutter method channel features. To know more about the flutter method channel, read the official documentation of the flutter method channel.