Flutter 插件:调用 iOS 和 Android 方法,包括无效的参数
Flutter plugin: invoking iOS and Android method including parameters not working
尝试我的第一个 Flutter 插件,我尝试在 iOS 和 Android 世界中调用一个方法。我成功地能够在没有任何参数的情况下调用这样的方法。
但是现在我想调用一个有参数的方法。
对于 iOS,由于某种原因我无法让它工作。 (也许这只是我一直在监督的自动完成的事情,因为 VSCode 没有自动完成我的 Swift 代码)。但也许是另外一回事。请对此提供任何帮助。
这是我的代码:
我的库(Flutter-world)是这样的:
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;
}
}
.
我的 swift 插件(iOS-world)看起来像这样:
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")
}
}
}
错误消息说:
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"]
这看起来像是 swift 语法错误。
你想做let someInfo1 : String = args[“someInfo1”]
在 miguelpruivo 的帮助下,我找到了解决方案。
这是工作代码:
Dart 中的 Flutter 世界是正确的:
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': 3.22,
});
return version;
}
}
.
在下面,Swift 中的 iOS-world - 现在也可以正常工作...
(Dart的dynamic
对应Swift的Any
)
(方法参数是 [String:Any]
类型的字典 - 有点像 Swift 经常使用的 userInfo - 因此你需要在接收处理程序中转换)...
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 {
return
}
if let myArgs = args as? [String: Any],
let someInfo1 = myArgs["someInfo1"] as? String,
let someInfo2 = myArgs["someInfo2"] as? Double {
result("Params received on iOS = \(someInfo1), \(someInfo2)")
} else {
result(FlutterError(code: "-1", message: "iOS could not extract " +
"flutter arguments in method: (sendParams)", details: nil))
}
} else if call.method == "getPlatformVersion" {
result("Running on: iOS " + UIDevice.current.systemVersion)
} else {
result(FlutterMethodNotImplemented)
}
}
}
尝试我的第一个 Flutter 插件,我尝试在 iOS 和 Android 世界中调用一个方法。我成功地能够在没有任何参数的情况下调用这样的方法。
但是现在我想调用一个有参数的方法。
对于 iOS,由于某种原因我无法让它工作。 (也许这只是我一直在监督的自动完成的事情,因为 VSCode 没有自动完成我的 Swift 代码)。但也许是另外一回事。请对此提供任何帮助。
这是我的代码:
我的库(Flutter-world)是这样的:
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;
}
}
.
我的 swift 插件(iOS-world)看起来像这样:
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")
}
}
}
错误消息说:
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"]
这看起来像是 swift 语法错误。
你想做let someInfo1 : String = args[“someInfo1”]
在 miguelpruivo 的帮助下,我找到了解决方案。
这是工作代码:
Dart 中的 Flutter 世界是正确的:
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': 3.22,
});
return version;
}
}
.
在下面,Swift 中的 iOS-world - 现在也可以正常工作...
(Dart的dynamic
对应Swift的Any
)
(方法参数是 [String:Any]
类型的字典 - 有点像 Swift 经常使用的 userInfo - 因此你需要在接收处理程序中转换)...
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 {
return
}
if let myArgs = args as? [String: Any],
let someInfo1 = myArgs["someInfo1"] as? String,
let someInfo2 = myArgs["someInfo2"] as? Double {
result("Params received on iOS = \(someInfo1), \(someInfo2)")
} else {
result(FlutterError(code: "-1", message: "iOS could not extract " +
"flutter arguments in method: (sendParams)", details: nil))
}
} else if call.method == "getPlatformVersion" {
result("Running on: iOS " + UIDevice.current.systemVersion)
} else {
result(FlutterMethodNotImplemented)
}
}
}