Detect running platform

Flutter tips Published on

Flutter is amazing because it can run on so many different places like your phone (Android, iOS), computer (Windows, Linux, macOS), and even in a web browser.

Why Detect the Platform?

Sometimes you need your app to behave differently based on where it's running. Maybe a different layout for desktop or a feature only for mobile.

How to Detect It

Flutter gives you a simple way to figure this out using the foundation.dart library.

The Code

Here's a simple way to check if the app is on a desktop, mobile, or web platform:

import 'package:flutter/foundation.dart';

class PlatformUtils {
  static bool isDesktop => defaultTargetPlatform == TargetPlatform.windows ||
      defaultTargetPlatform == TargetPlatform.linux ||
      defaultTargetPlatform == TargetPlatform.macOS;

  static bool isMobile => defaultTargetPlatform == TargetPlatform.android ||
      defaultTargetPlatform == TargetPlatform.iOS;

  static bool isWeb => kIsWeb;
}
Important Note for Web

If you're building for the web, remember don't import dart:io. It won't work there. Instead, use package:flutter/foundation.dart which is web-friendly and provides defaultTargetPlatform and kIsWeb.

Using these simple checks helps you build apps that feel right on every platform!

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why can't I use dart:io when building for the web?

The `dart:io` library is designed for interacting with the operating system's I/O capabilities (like files, sockets, etc.) and is not available in web browsers. For web development, you should use web-compatible libraries.

What Flutter library should I use to detect the platform for web?

You should use `package:flutter/foundation.dart`. It provides properties like `defaultTargetPlatform` and `kIsWeb` that work correctly across all platforms, including the web.

What platforms does Flutter support?

Flutter supports building apps for Android, iOS, Web, Windows, Linux, and macOS.

Read more
You may also be interested in
Go Beyond Material: Add Custom Colors in your Flutter Theme  blog card image
Go Beyond Material: Add Custom Colors in your Flutter Theme
Published on 2025-05-12T08:49:42.146Z
Change flutter version  blog card image
Change flutter version
Published on 2025-05-12T12:23:31.879Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved