Skip to main content

How to Detect Platform and Web in Flutter

Detecting the platform and web in Flutter is easy with the help of some built-in functions. In this post, we’ll take a look at how to detect platforms and the web in Flutter.

As a developer, it’s important to know what platform or web your app is running on. This is because different platforms and web browsers have different capabilities. For example, not all platforms support certain features, such as geolocation or audio playback. Furthermore, some platforms and web browsers have different performance characteristics. Knowing the platform or web your app is running on helps you make decisions about which features to use and how to optimize your app for performance.

Flutter provides the Platform class which can be used to detect OS while providing kIsWeb to detect web.

  • kIsWeb: It returns true if the app is running on a web platform. This means that if you want to know whether or not your application will work on the web, you can use this constant to find out. This implementation takes advantage of JavaScript’s lack of integer support.
  • Platform: dart:io’s Platform class provides API parity with the framework. It can be sued to detect platforms: Android, iOS, Windows, Linux, and Mac.
import 'package:flutter/foundation.dart' show kIsWeb;
import 'dart:io' show Platform;

if (kIsWeb) {
  print("This is a website");
} 

if (Platform.isAndroid) {

} else if (Platform.isIOS) {

} else if (Platform.isFuchsia) {

} else if (Platform.isWindows) {

} else if (Platform.isMacOS) {

} else if (Platform.isLinux) {

}

By continuing to use the site, you agree to the use of cookies.