How can I detect if my Flutter app is running in the web?

FlutterDart

Flutter Problem Overview


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?

Flutter Solutions


Solution 1 - Flutter

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.
}

Solution 2 - Flutter

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";
   }

Solution 3 - Flutter

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"); }

Solution 4 - Flutter

you can use "kIsWeb" to do the job

 if(kIsWeb){
// DO SOMETHING
}else{
// DO ANOTHER THING
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionn1ksView Question on Stackoverflow
Solution 1 - FlutterWesty92View Answer on Stackoverflow
Solution 2 - FlutterDiego SanchezView Answer on Stackoverflow
Solution 3 - FlutterDevenView Answer on Stackoverflow
Solution 4 - FluttermNouhView Answer on Stackoverflow