rvr*_*r93 18 flutter flutter-web
我的颤振代码未在网络上运行。
我发现可以使用“bool kisweb”来检测平台。但是我的代码在“FirebaseAuth.instance”上失败了。这是否意味着我不能在网络上使用 Firebaseauth,因为它可能取决于 dart:io?
在调试模式下在 Chrome 上启动 lib\main.dart...调试服务监听 ws://127.0.0.1:54007/NghsYaNRLKE= 为网络编译???小部件库捕获的异常 ????????????????????????????????????????????? ??????????????? 构建 MultiProvider 时抛出了以下 UnsupportedError: Unsupported operation: Platform._operatingSystem 相关的导致错误的小部件是: MultiProvider org-dartlang-app:///packages/My_App/main.dart:30:10 抛出异常时,这是堆栈:包:build_web_compilers/src/dev_compiler/dart_sdk.js 3996:11
throw_ 包:build_web_compilers/src/dev_compiler/dart_sdk.js 57810:17 _operatingSystem 包:build_web_compilers/src/dev_compiler/dart_sdk.js 57859:27 获取操作系统包:build_web_compilers/src/dev_compiler/dart_sdk.js 包:build_web_compilers/src/dev_compiler/dart_sdk777sd build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 获取包:build_web_compilers/src/dev_compiler/dart_sdk.js 57796:26 获取 isIOS 包:build_web_compilers/src/dev_compiler/dart_sdk.js7 getcore/src/dev_compiler/dart_sdk.js 5020:firebase 5Cfirebase_app.dart 15:16
获取 defaultAppName 包:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 获取包:firebase_core/src%5Cfirebase_app.dart 51:57 获取实例包:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 获取包src%5Cfirebase_auth.dart 25:67
获取实例包:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 get internalCallback ???????????????????????? ?????????????????????????????????????????????????????? ?????????????????????????????? 退出
请帮我解决这个问题。
Que*_* CG 21
我重新打开这个问题以提供更合适的答案,现在可以在原生的 flutter 中使用:
import 'package:flutter/foundation.dart';
if ((defaultTargetPlatform == TargetPlatform.iOS) || (defaultTargetPlatform == TargetPlatform.android)) {
// Some android/ios specific code
}
else if ((defaultTargetPlatform == TargetPlatform.linux) || (defaultTargetPlatform == TargetPlatform.macOS) || (defaultTargetPlatform == TargetPlatform.windows)) {
// Some desktop specific code there
}
else {
// Some web specific code there
}
Run Code Online (Sandbox Code Playgroud)
小智 13
您可以使用try-catch块来防止异常中断流程:
bool kisweb;
try{
if(Platform.isAndroid||Platform.isIOS) {
kisweb=false;
} else {
kisweb=true;
}
} catch(e){
kisweb=true;
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*tel 10
由于库的实现性质dart:io- 它在网络上运行不佳(与 冲突dart:html)。Unsupported operation: Platform._operatingSystem这就是为什么您在网络上运行代码时会遇到这样的情况。Web 无法访问其中Platform.is..实现的“dart:io”。
对我来说,对于每个系统和每个使用 flutter 构建的应用程序都有效的解决方案如下所示:
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
class PlatformUtils {
static bool get isMobile {
if (kIsWeb) {
return false;
} else {
return Platform.isIOS || Platform.isAndroid;
}
}
static bool get isDesktop {
if (kIsWeb) {
return false;
} else {
return Platform.isLinux || Platform.isFuchsia || Platform.isWindows || Platform.isMacOS;
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先检查你是否在Web上运行(如果是则返回false,并为Web编写代码),然后检查其他操作系统。
我花了 4 个小时研究这个问题。所有已经提到的都是正确的。但是,我很高兴我也找到了一个适用于网络的有效解决方案:universal_io包。
请记住不要使用 1.0.1 版本,因为 v1.0.1 和其他版本无法正确检测 iOS。我可以通过降级到 0.2.0 版本来摆脱它。当您阅读这篇文章时,它可能已经修复,请在此处查看问题状态:https : //github.com/dint-dev/universal_io/issues/8
导入“包:universal_io/io.dart”;
然后只需使用Platform.operatingSystem例如
print('OS: ${Platform.operatingSystem}');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21094 次 |
| 最近记录: |