gpt4 book ai didi

android - flutter 插件 : invoking iOS and Android method including parameters not working

转载 作者:IT王子 更新时间:2023-10-29 07:09:46 24 4
gpt4 key购买 nike

尝试我的第一个 Flutter 插件时,我尝试在 iOS 和 Android 世界中调用一个方法。我成功地调用了这样一个没有任何参数的方法。

但现在我想调用一个有参数的方法。

对于 iOS,由于某种原因我无法让它工作。 (也许这只是我一直在监督的自动完成的事情,因为 VSCode 不会自动完成我的 Swift 代码)。但也许是另外一回事。请对此提供任何帮助。

这是我的代码:

我的库(F​​lutter-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")
}
}
}

错误消息说:

注意:在类型后面加参数,构造一个类型的值
String someInfo1 = args["someInfo1"]

注意:在类型后面加参数,构造一个类型的值
String someInfo2 = args["someInfo2"]

注意:使用'.self'来引用类型对象
String someInfo1 = args["someInfo1"]

注意:使用'.self'来引用类型对象
String someInfo2 = args["someInfo2"]

警告:“String.Type”类型的表达式未使用
String someInfo1 = args["someInfo1"]

警告:“String.Type”类型的表达式未使用
String someInfo2 = args["someInfo2"]

最佳答案

miguelpruivo的帮助下,我找到了解决方案。

这是工作代码:

Dart 中的 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': 3.22,
});
return version;
}
}

.

下面是 Swift 中的 iOS 世界——现在也能正常工作了……

(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)
}
}
}

关于android - flutter 插件 : invoking iOS and Android method including parameters not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53898765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com