tezvyn:

Dart's Platform Class: Querying the Host Environment

AI-drafted, machine-checkedSource: api.flutter.devbeginner

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.

Read the original → api.flutter.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.