Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported operation: Platform._operatingSystem

My flutter code isn't running on web.

I found that "bool kisweb" can be used to detect the platform. But my code is failing at "FirebaseAuth.instance". Does this mean I can't use Firebaseauth on web as it might be depending on 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

Please help me resolve this issue.

like image 209
rvr93 Avatar asked Oct 19 '19 00:10

rvr93


4 Answers

I reopen this issue to give a more appropriate answer which is now available in native in 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
}
like image 68
Quentin CG Avatar answered Nov 20 '22 08:11

Quentin CG


You can use a try-catch block to prevent the exception from breaking the flow:

bool kisweb;
try{
    if(Platform.isAndroid||Platform.isIOS) {
        kisweb=false;
    } else {
        kisweb=true;
    }
} catch(e){
    kisweb=true;
}
like image 18
G.Zexin Avatar answered Nov 20 '22 08:11

G.Zexin


No, the FlutterFire group of plugins is in no way supported on Flutter Web. They rely on platform-specific APIs and are currently only implemented for Android and iOS.

Update May 2021: As pointed out by Moslem Deris, FlutterFire is now officially supported on the web: https://firebase.google.com/docs/flutter/setup?platform=web (thanks to Heikkisorsa for commenting the new link)

like image 8
Michael Pfaff Avatar answered Nov 20 '22 08:11

Michael Pfaff


I have spent last 4 hours researching this issue. All that was already mentioned is correct. However, I'm so happy I found a working solution for web, too: universal_io package.

Just remember not to use the 1.0.1 version cause v1.0.1 and other versions don't detect iOS correctly. I was able to go away with it by just downgrading to 0.2.0 version. By the time you read this post it may have been fixed, check out the issue status here: https://github.com/dint-dev/universal_io/issues/8

IMPLEMENTATION (super simple)

import 'package:universal_io/io.dart';

then just use Platform.operatingSystem e.g.

print('OS: ${Platform.operatingSystem}');
like image 8
Tomas Baran Avatar answered Nov 20 '22 08:11

Tomas Baran