【问题标题】:How to use passed parameters in swift setMethodCallHandler - self?.methodName(result: result)如何在 swift setMethodCallHandler 中使用传递的参数 - self?.methodName(result: result)
【发布时间】:2019-12-31 00:45:44
【问题描述】:

我正在尝试通过方法通道将 Flutter 中 onPressed 函数中的参数发送到 Swift,并将这些参数作为参数传递给被调用的 Swift 方法。

我的任务是创建一个调用用 Swift 编写的控制器的 Flutter UI。我在这里看到过使用 Flutter 中 platform.invokeMethod 发送的参数的示例,但它们不会在 Swift 端的 setMethodCallHandler 方法内的方法调用中使用这些参数。如果call.method 与我发送的内容匹配,我不知道如何在我想要调用的方法中包含这些参数。

1.main.dart中的按钮小部件:

    RaisedButton(
          child: Text('Get a Number'),
          onPressed: () => _getNumber(6, 2),
        )

2。 main.dart 中的异步 Flutter 函数:

    Future<void> _getNumber(int number, int times) async {
String numberText;
try {
  final int result = await platform.invokeMethod('multiply', <String, dynamic>{
    "number": number,
    "times": times
  });
  numberText = "Your number is $result .";
} on PlatformException catch (e) {
  numberText = "Failed to get a number: '${e.message}'.";
}

setState(() {
  _numberText = numberText;
});}

3。我在 appDelegate.swift 中的方法调用处理程序:

    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let customChannel = FlutterMethodChannel(name: "samples.flutter.dev",
                                            binaryMessenger: controller)
    customChannel.setMethodCallHandler({
    [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in

    switch call.method {
      case "getBatteryLevel":
        self?.receiveBatteryLevel(result: result)
      case "multiply":
        self?.multiply(result: result)
      default:
        result(FlutterMethodNotImplemented)
    }

4。我处理这个特定乘法的私有函数:

    private func multiply(result: FlutterResult) {
         // here I want to access the "number" and "times"
        // arguments that I passed in from Flutter
      result(Int(number * times))
    }

由于我对 swift 和 dart 还很陌生,所以我很难弄清楚如何导航/使用 result: FlutterResult。如何更改该参数或访问其属性以使用我传递给 Flutter 中的 onPressed 函数的 numbertimes 参数?

【问题讨论】:

    标签: swift asynchronous flutter dart


    【解决方案1】:

    更改您的 multiply 方法以像这样开始 - 将 call 传入:

    private func multiply(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
      if let args = call.arguments as? Dictionary<String, Any>,
        let number = args["number"] as? Int,
        let times = args["times"] as? Int {
        // use number and times as required, for example....
        result(number * times) // or your syntax
      } else {
        result(FlutterError.init(code: "bad args", message: nil, details: nil))
      }
    

    【讨论】:

    • 感谢@Richard Heap!你的回答帮助很大。我对您的答案进行了一些编辑,以显示最终对我有用的内容。例如,我无法使用 result(number * times) 不使用 as! 对 args 进行类型转换! (斯威夫特说你不能将 * 与两个 Int 一起使用?...)如果这种做法有问题,请随时再次编辑(但我不知道如何使这项工作发挥作用)。
    【解决方案2】:

    检查call 对象。它应该有一个arguments 属性。不幸的是,我找不到 FlutterMethodCall 的任何在线 API 文档,但那是可以查看的地方。

    【讨论】:

    • 感谢您的提示!我想我太菜鸟了,但我尝试检查调用没有成功,检查 FlutterMethodCall 只是给出了它是什么的一般描述(命令对象表示 FlutterMethodChannel 上的方法调用。)我还发了推文Flutter 团队,因为他们的文档当前指向 FlutterMethodCall 的 404 页面 (api.flutter.dev/objcdoc/Classes/FlutterMethodChannel.html)
    猜你喜欢
    • 2016-04-04
    • 2022-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 2019-09-14
    • 2012-09-25
    • 2019-08-07
    • 2013-04-25
    相关资源
    最近更新 更多