我正在尝试找到一种方法来查看我的 flutter web 项目正在哪个平台(iOS 或 Android)上运行。当我使用时if (Platform.isIOS) { //do something }
,我遇到以下错误:
Error: Unsupported operation: Platform._operatingSystem
at Object.throw_ [as throw] (http://localhost:44159/dart_sdk.js:4334:11)
at Function._operatingSystem (http://localhost:44159/dart_sdk.js:55064:17)
at Function.get operatingSystem [as operatingSystem] (http://localhost:44159/dart_sdk.js:55110:27)
at get _operatingSystem (http://localhost:44159/dart_sdk.js:55023:27)
at Function.desc.get [as _operatingSystem] (http://localhost:44159/dart_sdk.js:4819:15)
at get isIOS (http://localhost:44159/dart_sdk.js:55047:26)
at Function.desc.get [as isIOS] (http://localhost:44159/dart_sdk.js:4819:15)
at http://localhost:44159/packages/itoll/main.dart.lib.js:364:33
at wrapper (http://localhost:44159/dart_sdk.js:59904:30)
at http://localhost:44159/:70:11123
Run Code Online (Sandbox Code Playgroud)
有办法吗?
谢谢。
我想在 flutter 中编写特定于平台的代码,根据 flutter 文档,我们应该重写configureFlutterEngine方法,如下面的代码片段:
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
// Note: this method is invoked on the main thread.
// TODO
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我将flutterEngine传递给generatedPluginRegistrant.registerWith()时,它说:
类型不匹配:推断类型为 FlutterEngine,但为 PluginRegistry!预计
我已经检查了 github 中下面的链接,但没有找到任何适合我的解决方案。 https://github.com/flutter/flutter/issues/45231
这是用于编写特定于平台的代码的 flutter 文档的链接。 https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-kotlin-tab#example-project