如何将参数从Swift本机代码``放''到Flutter [英] How to 'put' arguments from Swift native code to Flutter
问题描述
我试图将参数从Swift的回调方法传递给Flutter。这是我想在本机Java代码中实现的示例:
I am trying to pass arguments from a callback method in Swift to Flutter. This is an example of what I want to achieve, in my native Java code:
@Override
public void onRewardRequest(final TJPlacement tjPlacement, final TJActionRequest tjActionRequest, final String itemId, final int quantity) {
this.registrar.activity().runOnUiThread(new Runnable() {
@Override
public void run() {
Map<String, Object> arguments = new HashMap<>();
arguments.put("requestId", tjActionRequest.getRequestId());
arguments.put("token", tjActionRequest.getToken());
arguments.put("itemId", itemId);
arguments.put("quantity", quantity);
channel.invokeMethod("onRewardRequest", arguments);
}
});
}
编辑:我面临一个问题,参数 args
是未定义的,我不确定上面Java代码中 arguments.put()
的Swift等效形式是什么。这是我当前的实现:
I am facing an issue, the argument args
is undefined and I'm unsure what is the Swift equivalent of arguments.put()
from the Java code above. This is my current implementation:
//Calling my method 'onRewardRequest()' from Dart->Swift
methodChannel.invokeMethod("onRewardRequest", arguments: args, result: {(r:Any?) -> () in
func placement(_ placement: TJPlacement?, didRequestReward request: TJActionRequest?,itemId: String?,quantity: Int) {
//How do I call `arguments.put` over here like shown above in the Java code?
}
})
推荐答案
看起来您想从Swift调用Dart方法,并传递一个字典的参数(将在Dart端变为Map)。创建一个相关类型的字典,例如:String-> Any,填充它并将其用作 arguments
参数。
It looks like you want to call a Dart method from Swift, passing an argument of a dictionary (will become a Map at the Dart end). Create a dictionary of the relevant type, for example: String->Any, populate it and use that as the arguments
parameter.
var args = [String: Any]()
args["itemId"] = itemId
args["quantity"] = quantity
// todo - add other arg members
methodChannel.invokeMethod("onRewardRequest", arguments: args)
这篇关于如何将参数从Swift本机代码``放''到Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!