Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter Web throws on Platform.is* #36126

Closed
rostopira opened this issue Jul 13, 2019 · 43 comments
Closed

Flutter Web throws on Platform.is* #36126

rostopira opened this issue Jul 13, 2019 · 43 comments
Labels
customer: crowd Affects or could affect many people, though not necessarily a specific customer. customer: webeap dependency: dart Dart team may need to help us found in release: 3.19 Found to occur in 3.19 found in release: 3.20 Found to occur in 3.20 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P3 Issues that are less important to the Flutter project platform-web Web applications specifically r: fixed Issue is closed as already fixed in a newer version team-web Owned by Web platform team triaged-web Triaged by Web platform team

Comments

@rostopira
Copy link
Contributor

rostopira commented Jul 13, 2019

Using master branch

Flutter 1.8.2-pre.30 • channel master • https://github.com/flutter/flutter.git
Framework • revision 13382f41c0 (4 hours ago) • 2019-07-12 23:24:04 -0700
Engine • revision 919e353236
Tools • Dart 2.5.0 (build 2.5.0-dev.1.0 24725a8559)

Sample code:

void main() async {
    var isAndroid = Platform.isAndroid; //Crashes right here
    //...
}
flutter run -d chrome
Uncaught (in promise) Error: Unsupported operation: Platform._operatingSystem
    at Object.throw_ [as throw] (dart_sdk.js:3995)
    at Function._operatingSystem (dart_sdk.js:43981)
    at Function.get operatingSystem [as operatingSystem] (dart_sdk.js:44030)
    at get _operatingSystem (dart_sdk.js:43943)
    at Function.desc.get [as _operatingSystem] (dart_sdk.js:5024)
    at get isAndroid (dart_sdk.js:43964)
    at Function.desc.get [as isAndroid] (dart_sdk.js:5024)
    at main$ (main.dart:11)
    at main$.next (<anonymous>)
    at runBody (dart_sdk.js:28865)
    at Object._async [as async] (dart_sdk.js:28893)
    at Object.main$ [as main] (main.dart:9)
    at main (main_web_entrypoint.dart:6)
    at main.next (<anonymous>)
    at onValue (dart_sdk.js:28846)
    at async._RootZone.new.runUnary (dart_sdk.js:28734)
    at _FutureListener.then.handleValue (dart_sdk.js:24660)
    at handleValueCallback (dart_sdk.js:25127)
    at Function._propagateToListeners (dart_sdk.js:25159)
    at async._AsyncCallbackEntry.new.callback (dart_sdk.js:24913)
    at Object._microtaskLoop (dart_sdk.js:28945)
    at _startMicrotaskLoop (dart_sdk.js:28951)
    at dart_sdk.js:25361
@jonahwilliams
Copy link
Member

We definitely need some better documentation describing this issue, but the gist is that dart:io is not really supported when compiling for the web. Instead of using Platform.isAndroid, you will be able to use either defaultTargetPlatform or Theme.platform. I say will, because the browser detection for Android/iOS is not yet implemented for the web (see https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/_platform_web.dart).

@jonahwilliams jonahwilliams added the platform-web Web applications specifically label Jul 13, 2019
@rostopira
Copy link
Contributor Author

rostopira commented Jul 13, 2019

Well, but could it don't crash at least? Or always return false? Or at least throw exception that could be caught with try/catch?
Even pure dart packages tend to use Platform.is* sometimes

Also, is there are anyway to detect running on web?

@jonahwilliams
Copy link
Member

Always returning false would be wrong though, you could very well be on an Android phone.

@jonahwilliams
Copy link
Member

See #36135 for a technique to detect running on the web

@yjbanov yjbanov added the dependency: dart Dart team may need to help us label Jul 18, 2019
@yjbanov yjbanov added this to the Goals milestone Jul 18, 2019
@yjbanov
Copy link
Contributor

yjbanov commented Jul 18, 2019

/cc @kevmoo @vsmenon

@kevmoo
Copy link
Contributor

kevmoo commented Jul 18, 2019

Always returning false would be wrong though, you could very well be on an Android phone.

Need to be VERY careful here.

Do we want to introduce an orthogonal concept of isWeb vs isAndroid.

Worth a discussion doc at the least.

@kevmoo
Copy link
Contributor

kevmoo commented Jul 18, 2019

I'd vote for introducing platform.dart in flutter to handle this case and to start migrating folks to it.

I believe this is what we discussed WRT platform-specific code, right @jonahwilliams ?

@jonahwilliams
Copy link
Member

What we have today is conditional based:

https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/_platform_io.dart
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/_platform_web.dart

@vsmenon
Copy link

vsmenon commented Jul 25, 2019

Note, we'd prefer disallowing importing dart:io altogether on the Web - i.e., give a compile time error. Today (at least for now), we allow importing, but throw at runtime.

We should be able to prevent imports for Flutter Web at least by editing Flutter's version of libraries.json.

@vsmenon
Copy link

vsmenon commented Aug 8, 2019

Upstream issue:

dart-lang/sdk#35969

@KodyDevSAGAR
Copy link

is there any workaround for the same ? still facing the same problem. Caught error: Unsupported operation: Platform._operatingSystem

@TannerYoung
Copy link

Just want to add that this is thrown by https://pub.dev/packages/google_sign_in as well if attempted to use with the flutter web beta version 1.13.1-pre.99

@bean5
Copy link

bean5 commented Feb 15, 2020

Dupe of #50843. This adds more information to the table, but its title doesn't follow the template notation regarding web bugs, so it might get overlooked.

@esDotDev
Copy link

esDotDev commented Mar 5, 2020

Is this really a dupe? The other bug is asking about package_info, this is just about the Platform.is booleans. It's pretty crazy that our builds are blowing up just because we're trying to check a static boolean. Why can't this just be set at build time?

Currently there's literally no way to check what platform you are on without blowing up on web. Which is kinda crazy for a platform aiming to be universal.

@bean5
Copy link

bean5 commented Mar 8, 2020

Precisely why I think this is strange problem to have. In my very effort to support web, my stuff crashes. It might even build. I'm using janky work-arounds that depend on context instead. Very annoying to have to pass context to a static tool I built for detection that would otherwise just call this instead. In other words, I'm not using this because it breaks web support.

@esDotDev
Copy link

esDotDev commented Mar 8, 2020

fwiw, either of these packages can be used to workaround the platform detection:
https://pub.dev/packages/universal_io
https://pub.dev/packages/universal_platform

@bean5
Copy link

bean5 commented May 8, 2020

Seems strange to depend on another project for something like this. Maybe when Flutter for web is officially supported it won't be necessary. One can hope.

@talhashahab786
Copy link

talhashahab786 commented May 19, 2020

use kIsWeb

import 'package:flutter/foundation.dart';

if (!kIsWeb) FlutterStatusbarcolor.setStatusBarColor(Color(redLong)); //code that runs for mobile

@esDotDev
Copy link

esDotDev commented Mar 14, 2021

You don't need to install any additional package.

Just do this instead to check if flutter is executed in the web browser:
import 'package:flutter/foundation.dart' show kIsWeb;

Usage:
if (kIsWeb) { print('do this'); } else { print('do that'); }

The issue is going the other way... if MacOS do something, or if Windows do something, you then can no longer run the codebase in the browser.

It really defies comprehension that the upstream never tries fixing the strange problem or giving any explicit explanation on development guides since 2019. 😕
Even falling back on "default" or null will be a much more practicable solution.

Agreed it is extremely odd to throw exceptions on a bunch of static bools, and to not simply figure out a practical solution for 2 yrs.

Then again, I wrote UniversalPlatform and it works pretty good :)

@tpal-dev
Copy link

tpal-dev commented Mar 28, 2021

This code should help in situation when you use windows size plugin for desktop size and on web throw error "Error: Unsupported operation: Platform._operatingSystem..."

in main.dart file you have to check if it is not a web mode, then check if Platform.is..:

import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:window_size/window_size.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  if (!kIsWeb) {
    if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
      setWindowMinSize(const Size(400, 600));
      setWindowMaxSize(const Size(800, double.infinity));
      setWindowFrame(Rect.fromLTRB(0, 0, 600.0, 1000.0));
    }
  }

  runApp(MyApp());
}

@lukepighetti
Copy link

lukepighetti commented Mar 29, 2021

We just had our web app explode because we added a Platform.isWindows check to avoid initializing Firebase services. Is there any solution available for this issue? This is a very unfortunate limitation if not.

EDIT: we did this with if(kIsWeb || !Platform.isWindows) initialize firebase

@omartinma
Copy link

Any update on that? I have a web app that I need to check if I'm running it on and mobile or desktop browser

@escamoteur
Copy link
Contributor

Just ran into this myself. Indeed this should not crash.

@FernandoUFS
Copy link

And actually I'm not able to create a web version of my app because some dependencies are checking Platform.isAndroid and Platform.isIOS without check kIsWeb. I think this would be important when launching flutter 2.0 with web at "stable" channel. I only have to wait for this to work on web to launch the web version.

@creativecreatorormaybenot
Copy link
Contributor

@FernandoUFS You can contribute to the dependencies :)

@esDotDev
Copy link

esDotDev commented Jun 1, 2021

And actually I'm not able to create a web version of my app because some dependencies are checking Platform.isAndroid and Platform.isIOS without check kIsWeb. I think this would be important when launching flutter 2.0 with web at "stable" channel. I only have to wait for this to work on web to launch the web version.

That's why I wrote this: https://pub.dev/packages/universal_platform

@AdrienLemaire
Copy link

AdrienLemaire commented Aug 3, 2022

The beginner's codelab uses Platform.is* and crashes with the same error

https://codelabs.developers.google.com/codelabs/flutter-boring-to-beautiful#2

source:
https://github.com/flutter/codelabs/blob/main/boring_to_beautiful/step_01/lib/main.dart#L16

It would be nice to have bug-free codelabs :) PR added at flutter/codelabs#1053

@danagbemava-nc
Copy link
Member

Reproducible on the latest versions of flutter.

Updating labels

flutter doctor -v
[✓] Flutter (Channel stable, 3.19.0, on macOS 14.3.1 23D60 darwin-arm64, locale en-GB)
    • Flutter version 3.19.0 on channel stable at /Users/nexus/dev/sdks/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (6 days ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/nexus/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode-15.2.0.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Users/nexus/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5)
    • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 77.2.2
    • Dart plugin version 232.10286

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (4 available)
    • Dean’s iPad (mobile) • 00008103-000825C811E3401E • ios            • iOS 17.3.1 21D61
    • Nexus (mobile)       • 00008020-001875E83A38002E • ios            • iOS 17.3.1 21D61
    • macOS (desktop)      • macos                     • darwin-arm64   • macOS 14.3.1 23D60 darwin-arm64
    • Chrome (web)         • chrome                    • web-javascript • Google Chrome 121.0.6167.184

[✓] Network resources
    • All expected network resources are available.

• No issues found!
[!] Flutter (Channel master, 3.20.0-10.0.pre.50, on macOS 14.3.1 23D60 darwin-arm64, locale en-GB)
    • Flutter version 3.20.0-10.0.pre.50 on channel master at /Users/nexus/dev/sdks/flutters
    ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2d422a3261 (7 hours ago), 2024-02-19 21:49:05 -0800
    • Engine revision e96c18b6c5
    • Dart version 3.4.0 (build 3.4.0-155.0.dev)
    • DevTools version 2.33.0-dev.6
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/nexus/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode-15.2.0.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Users/nexus/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5)
    • IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 77.2.2
    • Dart plugin version 232.10286

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (5 available)
    • Dean’s iPad (mobile)            • 00008103-000825C811E3401E • ios            • iOS 17.3.1 21D61
    • Nexus (mobile)                  • 00008020-001875E83A38002E • ios            • iOS 17.3.1 21D61
    • macOS (desktop)                 • macos                     • darwin-arm64   • macOS 14.3.1 23D60 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad     • darwin         • macOS 14.3.1 23D60 darwin-arm64
    • Chrome (web)                    • chrome                    • web-javascript • Google Chrome 121.0.6167.184

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

@yjbanov
Copy link
Contributor

yjbanov commented Mar 28, 2024

We did what we could on the Flutter side (#36126 (comment)). The remaining work, if any, is in the Dart SDK (see details in this closed issue: dart-lang/sdk#35969).

Closing as fixed.

@yjbanov yjbanov closed this as completed Mar 28, 2024
@danagbemava-nc danagbemava-nc added the r: fixed Issue is closed as already fixed in a newer version label Apr 1, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
customer: crowd Affects or could affect many people, though not necessarily a specific customer. customer: webeap dependency: dart Dart team may need to help us found in release: 3.19 Found to occur in 3.19 found in release: 3.20 Found to occur in 3.20 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P3 Issues that are less important to the Flutter project platform-web Web applications specifically r: fixed Issue is closed as already fixed in a newer version team-web Owned by Web platform team triaged-web Triaged by Web platform team
Projects
None yet
Development

No branches or pull requests