从本地iOS调用扑动,并使用鸽子flutterapi恢复价值

发布于 2025-02-11 16:18:40 字数 1308 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2025-02-18 16:18:40

您仅同步返回数据,来自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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文