不支持的操作:Platform._operatingSystem

Unsupported operation: Platform._operatingSystem

我的 flutter 代码不在网络上 运行。

我发现"bool kisweb"可以用来检测平台。但是我的代码在 "FirebaseAuth.instance" 处失败了。这是否意味着我不能在网络上使用 Firebaseauth,因为它可能取决于 dart:io?

Launching lib\main.dart on Chrome in debug mode... Debug service listening on ws://127.0.0.1:54007/NghsYaNRLKE= compiled for web ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following UnsupportedError was thrown building MultiProvider: Unsupported operation: Platform._operatingSystem The relevant error-causing widget was: MultiProvider org-dartlang-app:///packages/My_App/main.dart:30:10 When the exception was thrown, this was the stack: package:build_web_compilers/src/dev_compiler/dart_sdk.js 3996:11
throw_ package:build_web_compilers/src/dev_compiler/dart_sdk.js 57810:17 _operatingSystem package:build_web_compilers/src/dev_compiler/dart_sdk.js 57859:27 get operatingSystem package:build_web_compilers/src/dev_compiler/dart_sdk.js 57772:27 get _operatingSystem package:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 get package:build_web_compilers/src/dev_compiler/dart_sdk.js 57796:26 get isIOS package:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 get package:firebase_core/src%5Cfirebase_app.dart 15:16
get defaultAppName package:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 get package:firebase_core/src%5Cfirebase_app.dart 51:57 get instance package:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 get package:firebase_auth/src%5Cfirebase_auth.dart 25:67
get instance package:build_web_compilers/src/dev_compiler/dart_sdk.js 5020:17 get internalCallback ════════════════════════════════════════════════════════════════════════════════════════════════════ Exited

请帮我解决这个问题。

不,FlutterWeb 不支持 FlutterFire 插件组。它们依赖于特定于平台的 API,目前仅针对 Android 和 iOS.

实施

2021 年 5 月更新:正如 Moslem Deris 指出的那样,FlutterFire 现在在网络上得到正式支持:https://firebase.flutter.dev/docs/installation/web/

您可以使用 try-catch 块来防止异常中断流程:

bool kisweb;
try{
    if(Platform.isAndroid||Platform.isIOS) {
        kisweb=false;
    } else {
        kisweb=true;
    }
} catch(e){
    kisweb=true;
}

对我来说,转移到开发频道使用 flutter channel dev

大多数 Firebase 插件现在都支持 Web。

我花了最后 4 个小时研究这个问题。所有已经提到的都是正确的。但是,我很高兴我也找到了一个适用于网络的有效解决方案:universal_io package.

请记住不要使用 1.0.1 版本,因为 v1.0.1 和其他版本无法正确检测 iOS。我可以通过降级到 0.2.0 版本来摆脱它。当您阅读本文时 post 它可能已经修复,请在此处查看问题状态:https://github.com/dint-dev/universal_io/issues/8

实现(超级简单)

导入'package:universal_io/io.dart';

然后只需使用 Platform.operatingSystem 例如

print('OS: ${Platform.operatingSystem}');

我重新打开这个问题来给出一个更合适的答案,现在可以在 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
}

当您首先检查 web if 条件时不会出现错误,请尝试以下方法,对我有用。

if (kIsWeb) {
      //running on web
} else if (Platform.isAndroid || Platform.isIOS) {
      //running on android or ios device
}

或者如果你想使用,你可以添加 try catch 块。

try {
   if (Platform.isAndroid || Platform.isIOS) {
      //running on android or ios device
   }
} catch (e) {
   if (kIsWeb) {
      //running on web
   }
}

您必须在 web/index.html 文件和 main.dart 文件中初始化 Firebase。

web/index.html 文件的 body 标签后添加此代码

<body>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
<script>
  const firebaseConfig = {
    apiKey: "YOUR_API_KEY",
    authDomain: "domain.firebaseapp.com",
    projectId: "your_project_id",
    storageBucket: "your.appspot.com",
    messagingSenderId: "1777...",
    appId: "1:1674...appId"
  };

  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
</script>

更多信息https://firebase.flutter.dev/docs/installation/web/

同时修改main.dart文件

//import the dependencie 
import 'package:firebase_core/firebase_core.dart';

//modify the main method
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}

我遇到了同样的问题。在网络中,您必须设置元数据并使用 putData 而不是 putFile:

final metadata = firebase_storage.SettableMetadata(
    contentType: 'image/jpeg',
    customMetadata: {'picked-file-path': file.path});

if (kIsWeb) {
  uploadTask = ref.putData(await file.readAsBytes(), metadata);
} else {
  uploadTask = ref.putFile(io.File(file.path), metadata);
}

您可以在 https://pub.dev/packages/firebase_storage/example

中查看完整示例