How to pass arguments from Flutter to Kotlin?
Flutter
On the Flutter side, you can pass arguments by including them as a map in the invokeMethod
call.
_channel.invokeMethod('showToast', {'text': 'hello world'});
Kotlin
On the Kotlin side you can get the parameters by casting call.arguments
as a Map or getting a particular argument from call.argument()
.
override fun onMethodCall(call: MethodCall, result: Result) { when (call.method) { "showToast" -> { val text = call.argument<String>("text") // hello world showToast(text) } }}
Dart side (sending data)
var channel = MethodChannel('foo_channel');var dataToPass = <String, dynamic>{ 'os': 'Android',};await channel.invokeListMethod<String>('methodInJava', dataToPass);
Java side (receiving data):
if (methodCall.method.equals("methodInJava")) { // Get the entire Map. HashMap<String, Object> map = (HashMap<String, Object>) methodCall.arguments; Log.i("MyTag", "map = " + map); // {os=Android} // Or get a specific value. String value = methodCall.argument("os"); Log.i("MyTag", value); // Android}