从本地iOS调用扑动,并使用鸽子flutterapi恢复价值
我正在使用 pigeon 库库来连接颤音和本机平台代码,尤其是iOS,特别是swift。
我想从Swift调用弹奏函数,并同步获得值。
颤动上的函数是这样的:
@FlutterApi()
abstract class MyFlutterApi {
String? didSyncFunctionCalled();
}
此处的Swift代码:
let flutterApi: MyFlutterApi?
public func callSyncFunction() -> String? {
flutterApi?.didSyncFunctionCalled(completion: { (value: String?, error: Error?) in
return value
})
}
您可以看到该函数返回字符串(无效),并且我需要从flutter中的字符串。
此实现是不正确的。我得到 error 来自return> return> return value value
- > 无法转换类型的“字符串?”的值。要封闭结果类型“ void”
从我所理解的内容中,Pigeon总是在错误的情况下生成代码,并最终返回。
这是我不想使用的解决方案:
public func callSyncFunction(completion: @escaping (String?) -> Void) {
flutterApi?.didSyncFunctionCalled(completion: { (value: String?, error: Error?) in
completion(value)
})
}
是否有一种可以在本机代码中使用这样使用的函数的方法?
public func callSyncFunction() -> String? {
let value: String? = flutterApi?.didSyncFunctionCalled()
return value
}
I'm using Pigeon library to connect Flutter and native platform code, in particular iOS with Swift.
I want to call a flutter function from Swift and get a value back, all synchronously.
The function on flutter is defined like this:
@FlutterApi()
abstract class MyFlutterApi {
String? didSyncFunctionCalled();
}
Here the Swift code:
let flutterApi: MyFlutterApi?
public func callSyncFunction() -> String? {
flutterApi?.didSyncFunctionCalled(completion: { (value: String?, error: Error?) in
return value
})
}
As you can see the function returns a string (nullable) and I need that string from flutter.
This implementation is incorrect. I get an error from XCode on the line of return value
-> Cannot convert value of type 'String?' to closure result type 'Void'
From what I understood, Pigeon always generates code with completion closure with error and eventually a value to return.
This is the solution I don't want to use:
public func callSyncFunction(completion: @escaping (String?) -> Void) {
flutterApi?.didSyncFunctionCalled(completion: { (value: String?, error: Error?) in
completion(value)
})
}
Is there a method to define functions that can be used like this in native code?
public func callSyncFunction() -> String? {
let value: String? = flutterApi?.didSyncFunctionCalled()
return value
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仅同步返回数据,来自API的请求是异步。
所有这些都是基于方法渠道构建的,因此异步功能并没有消除更多的包装。
You only return data synchronously the requests from the API is async.
It is all built on MethodChannels so the async functionality did not dissapear its more of wrapped up.