Hi, thanks for the great tutorial. This is what exactly I am looking for. However, after following this tutorial, I still cannot invoke my flutter method from native android. Could you please provide the entire code example? I go to the link you put in this article but unfortunately, the code is not available anymore. And how about the return value of dart method is not void but some types such as String? How to get the value from native and display it from Android application?
Thanks for your prompt response. Waiting for the updated URLs :)
I have already checked the example in the repository mentioned in the previous comment. However, it is intended for the example that needs parameters in the method. What I need is something like this:
Dart side:
```
Class Hello()
{
String hello() {
return "Hello from flutter"
}
}
```
In dart plugin method channel:
```
Future<String> methodHandler(MethodCall call)
{
String message = ""
switch (call):
case: "hello"
{
message = Hello().hello()
break;
}
return message"
}
```
Now, how do I capture the return value in Android side? In this example, the return value is String. I know that the MethodChannel.invokeMethod has `Result` member in the form of callback. But I am not sure how to use it and display the result in existing android application that is not built in Flutter.
Now in Android code, the call.method property will have 'receiveMessage' and the call.arguments value will be Map<String,String>{'message': 'Hello from flutter')
In resume, to send data through platform channels we always use a map of arguments in
MethodChannel.invokeMehod(<method name>, <map of arguments>)
In this code we did a simple example without parameters. But we use in our iOS and Android SDKs a platform channel with parameters from native to dart and vice-versa.
In Dart code you need to use the 'call.arguments' property in methodHandler(MethodCall call). Take a look how we do it in the TIKI SDK Platform Channel
The same approach can be done from Flutter to native. You use the second parameter in Dart and the call.arguments property in the handler in native code.
Hi, thanks for the great tutorial. This is what exactly I am looking for. However, after following this tutorial, I still cannot invoke my flutter method from native android. Could you please provide the entire code example? I go to the link you put in this article but unfortunately, the code is not available anymore. And how about the return value of dart method is not void but some types such as String? How to get the value from native and display it from Android application?
Thanks a lot for the post and your help!
Hey!
Our repositories URLs changed. That is probably why the lonk is not working... I will update it.
Check this comment to see if it helps: https://blog.mytiki.com/p/how-to-call-dart-from-android-or/comment/16971766.
If you have any questions after that, let me know!
Hi Ricardo,
Thanks for your prompt response. Waiting for the updated URLs :)
I have already checked the example in the repository mentioned in the previous comment. However, it is intended for the example that needs parameters in the method. What I need is something like this:
Dart side:
```
Class Hello()
{
String hello() {
return "Hello from flutter"
}
}
```
In dart plugin method channel:
```
Future<String> methodHandler(MethodCall call)
{
String message = ""
switch (call):
case: "hello"
{
message = Hello().hello()
break;
}
return message"
}
```
Now, how do I capture the return value in Android side? In this example, the return value is String. I know that the MethodChannel.invokeMethod has `Result` member in the form of callback. But I am not sure how to use it and display the result in existing android application that is not built in Flutter.
Thanks again for your support and kindness!
In your methodHandler you should not return the value. Instead, use the MethodChannel.invokeMethod method to send the data back to Android.
Considering that you are using the default Fllutter Plugin class, your could would be something like this:
Future<String> methodHandler(MethodCall call) {
String helloMessage = Hello().hello();
methodChannel.invokeMethod('receiveMessage', <String, String>{
'message': helloMessage,
});
}
Now in Android code, the call.method property will have 'receiveMessage' and the call.arguments value will be Map<String,String>{'message': 'Hello from flutter')
In resume, to send data through platform channels we always use a map of arguments in
MethodChannel.invokeMehod(<method name>, <map of arguments>)
Great article and tutorial! However, is it possible to pass a parameter from native to our dart package?
for example i want to pass an integer (a &b) inside the plugin
channel.invokeMethod("sum",a,b null), but i got error..
Great question Farhan!
In this code we did a simple example without parameters. But we use in our iOS and Android SDKs a platform channel with parameters from native to dart and vice-versa.
In Dart code you need to use the 'call.arguments' property in methodHandler(MethodCall call). Take a look how we do it in the TIKI SDK Platform Channel
https://github.com/tiki/tiki-sdk-platform-channel/blob/76075bb38ed01dd7d8729af634bb6e6f3a2f7d67/lib/src/platform_channel/platform_channel.dart#L54
In native code you use the second argument of the invokeMethod as a Map in Android or Dictionary in iOS.
This is how we do it in Android: https://github.com/tiki/tiki-sdk-android/blob/main/app/src/main/kotlin/com/mytiki/tiki_sdk_android/core/CoreChannel.kt#L78-L8
and in iOS: https://github.com/tiki/tiki-sdk-ios/blob/main/Sources/TikiSdk/Core/CoreChannel.swift#L88-L92
The same approach can be done from Flutter to native. You use the second parameter in Dart and the call.arguments property in the handler in native code.