To invoke a native method you need 2 steps only
1. Create method channel and invoke
2. Implement method handler and set method handler
Step 1- For native method call first you need to create a class in lib folder of your plugin and initiate a MethodChannel.
class TestMethodCall {
static const MethodChannel _channel = const MethodChannel('cloud_sdk');
Future<bool> _initDevice( ) async {
try {
return _channel.invokeMethod('deviceMethod');
} on PlatformException catch (ex) {
print(ex);
}
return false;
}
}
static const MethodChannel _channel = const MethodChannel('cloud_sdk');
Future<bool> _initDevice( ) async {
try {
return _channel.invokeMethod('deviceMethod');
} on PlatformException catch (ex) {
print(ex);
}
return false;
}
}
Step 2- Now Create another class in your native Android src.main folder where you need to implement MethodCallHandler
public class CloudSdkPlugin implements MethodCallHandler {
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(),"cloud_sdk");
channel.setMethodCallHandler(new CloudSdkPlugin());
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if(call.method.equals("deviceMethod")){
/// TODO: Do your native work here
}
}
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(),"cloud_sdk");
channel.setMethodCallHandler(new CloudSdkPlugin());
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if(call.method.equals("deviceMethod")){
/// TODO: Do your native work here
}
}
}
*If you need to pass the arguments
class TestMethodCall {
static const MethodChannel _channel = const MethodChannel('cloud_sdk');
Future<bool> _initDevice(String userId,String userName) async {
try {
return _channel.invokeMethod('deviceMethod', {'userId': userId,'userName': userName});
} on PlatformException catch (ex) {
print(ex);
}
return false;
}
}
public class CloudSdkPlugin implements MethodCallHandler {
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(),"cloud_sdk");
channel.setMethodCallHandler(new CloudSdkPlugin());
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if(call.method.equals("deviceMethod")){
String userId = call.argument("userId");
String userName = call.argument("userName");
/// TODO: Do your native work here
}
}
}
static const MethodChannel _channel = const MethodChannel('cloud_sdk');
Future<bool> _initDevice(String userId,String userName) async {
try {
return _channel.invokeMethod('deviceMethod', {'userId': userId,'userName': userName});
} on PlatformException catch (ex) {
print(ex);
}
return false;
}
}
public class CloudSdkPlugin implements MethodCallHandler {
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(),"cloud_sdk");
channel.setMethodCallHandler(new CloudSdkPlugin());
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if(call.method.equals("deviceMethod")){
String userId = call.argument("userId");
String userName = call.argument("userName");
/// TODO: Do your native work here
}
}
}
ConversionConversion EmoticonEmoticon