144

I know that I can detect the operating system with Platform.isAndroid, Platform.isIOS, etc. but there isn't something like Platform.isWeb so how can I detect this?

n1ks
  • 2,188
  • 2
  • 12
  • 12
  • For more info you can check my answer in other question using below link. https://stackoverflow.com/a/70039641/9985458 – Deven Nov 19 '21 at 18:55

6 Answers6

281

There is a global boolean kIsWeb which can tell you whether or not the app was compiled to run on the web.

Documentation: https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

import 'package:flutter/foundation.dart' show kIsWeb;

if (kIsWeb) {
  // running on the web!
} else {
  // NOT running on the web! You can check for additional platforms here.
}
Westy92
  • 19,087
  • 4
  • 72
  • 54
  • 2
    if an app is compiled to run on the web and now I'm running an app on android/ios then this code can be executed or not? – Deven Dec 28 '19 at 18:35
  • will it give me the power with which I can get an app is running on the web, not in android/ios/software/windows? – Deven Dec 28 '19 at 18:36
  • 2
    Don't forget to import it... import 'package:flutter/foundation.dart'; – Miguel Pereira Jan 29 '20 at 04:00
  • Is it possible to detect desktop vs mobile? – Jeffin Manuel Jun 01 '20 at 12:32
  • 6
    it's very ironic that we already have `isFuchsia` and `isLinux` but I still had to come here to check on how to detect for web. – Fabio Jun 19 '20 at 12:05
  • Does this tell the difference between running on a mobile device as PWA and running on a desktop browser? – Eight Rice Sep 09 '20 at 16:15
  • @Deven the answer is: Yes, the snipped provided by Westy92 detects whether is running on Web or not. If not web, then you can check if is Windows, Linux, Android, IOS, etc, by import Platform class (https://api.flutter.dev/flutter/dart-io/Platform-class.html). – Alex Correia Feb 15 '21 at 23:06
  • Just what I was looking for as I've built an app for both mobile and web site, thanks – user2244131 Oct 04 '22 at 09:19
30

If you want to know what your OS is on the web, you can use

    String platform = "";
    if (kIsWeb) {
      platform = getOSInsideWeb();
    }

    String getOSInsideWeb() {
      final userAgent = window.navigator.userAgent.toString().toLowerCase();
      if( userAgent.contains("iphone"))  return "ios";
      if( userAgent.contains("ipad")) return "ios";
      if( userAgent.contains("android"))  return "Android";
    return "Web";
   }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Diego Sanchez
  • 1,369
  • 1
  • 11
  • 5
17

There is a code written Below to get OS/web where flutter is running...

if(kIsWeb)
   return Text("It's web");

else if(Platform.isAndroid){
     return Text("it's Android"); }
Deven
  • 684
  • 7
  • 10
10

you can use "kIsWeb" to do the job

 if(kIsWeb){
// DO SOMETHING
}else{
// DO ANOTHER THING
}
mNouh
  • 181
  • 1
  • 6
2

Just use this package get: ^4.6.5

import 'package:get/get.dart';

You can use this to find web

bool isWeb = GetPlatform.isWeb;

For Others

 bool isMobile = GetPlatform.isMobile;
 bool isAndroid = GetPlatform.isAndroid;
 bool isiOS = GetPlatform.isIOS;
 bool isWeb = GetPlatform.isWeb;
 bool isWindows = GetPlatform.isWindows;
 bool isMac = GetPlatform.isMacOS;
 bool isLinux = GetPlatform.isLinux;
 bool isFusia = GetPlatform.isFuchsia;
 bool isDesktop = GetPlatform.isDesktop;
Anand
  • 4,355
  • 2
  • 35
  • 45
0

In dart:

bool kIsWeb = bool.fromEnvironment('dart.library.js_util');

src: https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

Ali Bagheri
  • 3,068
  • 27
  • 28