androidflutterkotlindartflutter-method-channel

How to create list of Pairs?


I want to create some Pair in Dart (Flutter) e.g.

Pair('text', 'some text here')
Pair('my_value', '233')

and pass it to Android activity (kotlin) via MethodChannel

How Can I do that? I've create a Dart function which takes HashMap but I a bit lost in it and not sure how proceed with that.

 static Future<void> sendMessage({
    HashMap? customData
  }) async {
    _eventChannel.invokeMethod('message', {
      'customData': customData
    });
  }

Solution

  • Just use Map, something like this would work:

    final data = <String, dynamic>{'text': 'some text here'};
    data['my_value'] = '123';
    methodChannel.invokeMethod('method', data);
    
    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
      val arguments = call.arguments as HashMap<String, Any>
      val text = arguments["text"] as String
      val myValue = arguments["my_value"] as String
    }