Skip to content
tezvyn:
Menu

Dart's Platform Class: Querying the Host Environment

Source: api.flutter.devEasyHow cards are made

Think of Dart's Platform class as a runtime detective for your app. It answers "where am I running?" by checking the OS, environment variables, and more. Use it to show different UI on iOS vs. Android.

Why it exists

Apps need to adapt to the environment they run in. A desktop app on Windows needs to handle file paths with backslashes, while a mobile app might need to show native-looking UI controls for iOS or Android. The Platform class was created to give developers a single, unified API to query these environmental details from within their Dart code.

The mental model

The Platform class is a static utility from the dart:io library. Think of it as a read-only information desk for your running application. You can walk up to it at any time, without creating an instance, and ask questions like "Are we on a Mac?", "What's the value of the 'PATH' environment variable?", or "How many processors does this machine have?". It provides facts about the host system, not the app itself.

How it works

The Platform class exposes a set of static boolean getters and string properties. You can check if (Platform.isAndroid) to execute code only on Android devices. For more general OS information, Platform.operatingSystem returns a string like 'linux', 'macos', or 'windows'. It also provides access to system environment variables through Platform.environment, which returns an unmodifiable map of key-value pairs. Other useful properties include Platform.numberOfProcessors, Platform.localeName, and Platform.pathSeparator.

When to use it

Use Platform when your app's logic needs to change based on the operating system. A primary use case in Flutter is choosing between Material (Android-style) and Cupertino (iOS-style) widgets. It's also crucial for command-line Dart applications that need to read environment variables or construct file paths correctly for the host OS, for instance, by joining path components with Platform.pathSeparator.

When not to use it

The most critical limitation is that Platform is part of dart:io, which is not available in web environments. Attempting to access Platform.isIOS or any other property in a Flutter web app will throw an exception and crash the application. For platform-detection in code that might run on the web, you should first check the kIsWeb constant (from Flutter's foundation library) to see if you're in a browser before attempting to use any dart:io features.

One canonical example

A common Flutter pattern is to build a UI that adapts to the host OS. For example, to show a different style of button on iOS versus other platforms:

import 'dart:io' show Platform; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart';

// Inside a widget's build method:

if (Platform.isIOS) {
return CupertinoButton(child: Text('iOS Button'), onPressed: () {});
} else {
return ElevatedButton(child: Text('Generic Button'), onPressed: () {});
}

This code checks if the OS is iOS and returns a native-looking CupertinoButton. On all other platforms, like Android or desktop, it falls back to a Material Design ElevatedButton.

Interview question

What is a critical limitation when using Dart's Platform class in a Flutter application?

  • a.It can only query basic OS type (e.g., mobile, desktop), not specific versions or architectures.
  • b.It requires manual setup of environment variables to function correctly across different OS.
  • c.It is unavailable when your Flutter application is compiled for the web.Correct
  • d.It can only be used to detect the operating system, not to access environment variables.
Why?

The card explicitly states that Platform is part of dart:io, which is not available in web environments, making it a critical limitation for web-based Flutter apps. Options A, B, and D are incorrect as the class provides specific OS details, reads existing environment variables, and works without manual setup for its own function.

Just read this? Test yourself on what you have been reading.

Read the original → api.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles