Search code examples
androidswiftpluginsflutterinvoke

Flutter plugin: invoking iOS and Android method including parameters not working


Trying my first Flutter plugin, I try to invoke a method in both, the iOS and the Android-world. I successfully was able to invoke such a method without any parameters.

But now I would like to invoke a method that has parameters.

For iOS, I can't get it to work for some reason. (maybe it is just an autocomplete thing that I keep overseeing since VSCode is not autocompleting my Swift code). But maybe it is something else. Please any help on this.

Here is my code:

My lib (Flutter-world) looks like this:

import 'dart:async';
import 'package:flutter/services.dart';

class SomeName {
  static const MethodChannel _channel =
      const MethodChannel('myTestMethod');

  static Future<String> get sendParamsTest async {
    final String version = await _channel.invokeMethod('sendParams',<String, dynamic>{
        'someInfo1': "test123",
        'someInfo2': "hello",
      });
    return version;
  }
}

.

My swift plugin (iOS-world) looks like this:

import Flutter
import UIKit

public class SwiftSomeNamePlugin: NSObject, FlutterPlugin {

  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "myTestMethod", binaryMessenger: registrar.messenger())
    let instance = SwiftSomeNamePlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {

    // flutter cmds dispatched on iOS device :
    if call.method == "sendParams" {

      guard let args = call.arguments else {
        result("iOS could not recognize flutter arguments in method: (sendParams)") 
      }
      String someInfo1 = args["someInfo1"]
      String someInfo2 = args["someInfo2"]
      print(someInfo1)
      print(someInfo2)
      result("Params received on iOS = \(someInfo1), \(someInfo2)")
    } else {
      result("Flutter method not implemented on iOS")
    }
  }
}

The error messages say:

note: add arguments after the type to construct a value of the type String someInfo1 = args["someInfo1"]

note: add arguments after the type to construct a value of the type String someInfo2 = args["someInfo2"]

note: use '.self' to reference the type object String someInfo1 = args["someInfo1"]

note: use '.self' to reference the type object String someInfo2 = args["someInfo2"]

warning: expression of type 'String.Type' is unused String someInfo1 = args["someInfo1"]

warning: expression of type 'String.Type' is unused String someInfo2 = args["someInfo2"]


Solution

  • This looks like a swift syntax error.

    You want to do let someInfo1 : String = args[“someInfo1”]