Skip to content

Platform abstract final

abstract final class Platform

Information about the environment in which the current program is running.

Platform provides information such as the operating system, the hostname of the computer, the value of environment variables, the path to the running program, and other global properties of the program being run.

Get the URI of the current Dart script

Use the script getter to get the URI to the currently running Dart script.

dart
import 'dart:io' show Platform;

void main() {
  // Get the URI of the script being run.
  var uri = Platform.script;
  // Convert the URI to a path.
  var path = uri.toFilePath();
}

Get the value of an environment variable

The environment getter returns a the names and values of environment variables in a Map that contains key-value pairs of strings. The Map is unmodifiable. This sample shows how to get the value of the PATH environment variable.

dart
import 'dart:io' show Platform;

void main() {
  Map<String, String> envVars = Platform.environment;
  print(envVars['PATH']);
}

Determine the OS

You can get the name of the operating system as a string with the operatingSystem getter. You can also use one of the static boolean getters: isMacOS, isLinux, isWindows, etc.

dart
import 'dart:io' show Platform;

void main() {
  // Get the operating system as a string.
  String os = Platform.operatingSystem;
  // Or, use a predicate getter.
  if (Platform.isMacOS) {
    print('is a Mac');
  } else {
    print('is not a Mac');
  }
}

Properties

hashCode no setter inherited

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Object.

Implementation
dart
external int get hashCode;

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

Methods

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

toString() inherited

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Inherited from Object.

Implementation
dart
external String toString();

Operators

operator ==() inherited

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Object.

Implementation
dart
external bool operator ==(Object other);

Static Properties

environment no setter

Map<String, String> get environment

The environment for this process as a map from string key to string value.

The map is unmodifiable, and its content is retrieved from the operating system on its first use.

Environment variables on Windows are case-insensitive, so on Windows the map is case-insensitive and will convert all keys to upper case. On other platforms, keys can be distinguished by case.

Implementation
dart
static Map<String, String> get environment => _Platform.environment;

executable no setter

String get executable

The path of the executable used to run the script in this isolate. Usually dart when running on the Dart VM or the compiled script name (script_name.exe).

The literal path used to identify the executable. This path might be relative or just be a name from which the executable was found by searching the system path.

Use resolvedExecutable to get an absolute path to the executable.

Implementation
dart
static String get executable => _Platform.executable;

executableArguments no setter

List<String> get executableArguments

The flags passed to the executable used to run the script in this isolate.

These are the command-line flags to the executable that precedes the script name. Provides a new list every time the value is read.

Implementation
dart
static List<String> get executableArguments => _Platform.executableArguments;

isAndroid final

final bool isAndroid

Whether the operating system is a version of Android.

Implementation
dart
@pragma("vm:platform-const")
@pragma("vm:shared")
static final bool isAndroid = (operatingSystem == "android");

isFuchsia final

final bool isFuchsia

Whether the operating system is a version of Fuchsia.

Implementation
dart
@pragma("vm:platform-const")
@pragma("vm:shared")
static final bool isFuchsia = (operatingSystem == "fuchsia");

isIOS final

final bool isIOS

Whether the operating system is a version of iOS.

Implementation
dart
@pragma("vm:platform-const")
@pragma("vm:shared")
static final bool isIOS = (operatingSystem == "ios");

isLinux final

final bool isLinux

Whether the operating system is a version of Linux.

This value is false if the operating system is a specialized version of Linux that identifies itself by a different name, for example Android (see isAndroid).

Implementation
dart
@pragma("vm:platform-const")
@pragma("vm:shared")
static final bool isLinux = (operatingSystem == "linux");

isMacOS final

final bool isMacOS

Whether the operating system is a version of macOS.

Implementation
dart
@pragma("vm:platform-const")
@pragma("vm:shared")
static final bool isMacOS = (operatingSystem == "macos");

isWindows read / write

bool isWindows

getter:

Whether the operating system is a version of Microsoft Windows.

setter:

Whether the operating system is a version of Microsoft Windows.

Implementation
dart
@pragma("vm:platform-const")
@pragma("vm:shared")
static bool isWindows = (operatingSystem == "windows");

lineTerminator no setter

String get lineTerminator

The current operating system's default line terminator.

The default character sequence that the operating system uses to separate or terminate text lines.

The line terminator is currently the single line-feed character, U+000A or "\n", on all supported operating systems except Windows, which uses the carriage-return + line-feed sequence, U+000D U+000A or "\r\n"

Implementation
dart
@pragma("vm:platform-const")
static String get lineTerminator => isWindows ? '\r\n' : '\n';

localeName no setter

String get localeName

Get the name of the current locale.

The result usually consists of

  • a language (e.g., "en"), or
  • a language and country code (e.g. "en_US", "de_AT"), or
  • a language, country code and character set (e.g. "en_US.UTF-8").

On macOS and iOS, the locale is taken from CFLocaleGetIdentifier.

On Linux and Fuchsia, the locale is taken from the "LANG" environment variable, which may be set to any value. For example:

shell
LANG=kitten dart myfile.dart  # localeName is "kitten"

On Android, the value will not change while the application is running, even if the user adjusts their language settings.

See https://en.wikipedia.org/wiki/Locale_(computer_software)

Implementation
dart
static String get localeName => _Platform.localeName();

localHostname final

final String localHostname

The local hostname for the system.

For example: "mycomputer.corp.example.com" "mycomputer"

Uses the platform gethostname implementation.

Implementation
dart
@pragma("vm:shared")
static final localHostname = _Platform.localHostname;

numberOfProcessors final

final int numberOfProcessors

The number of individual execution units of the machine.

Implementation
dart
static final numberOfProcessors = _Platform.numberOfProcessors;

operatingSystem final

final String operatingSystem

A string representing the operating system or platform.

Possible values include:

  • "android"
  • "fuchsia"
  • "ios"
  • "linux"
  • "macos"
  • "windows"

Note that this list may change over time so platform-specific logic should be guarded by the appropriate Boolean getter e.g. isMacOS.

Implementation
dart
@pragma("vm:platform-const")
@pragma("vm:shared")
static final operatingSystem = _Platform.operatingSystem;

operatingSystemVersion final

final String operatingSystemVersion

A string representing the version of the operating system or platform.

The format of this string will vary by operating system, platform and version and is not suitable for parsing. For example: "Linux 5.11.0-1018-gcp #20~20.04.2-Ubuntu SMP Fri Sep 3 01:01:37 UTC 2021" "Version 14.5 (Build 18E182)" '"Windows 10 Pro" 10.0 (Build 19043)'

Implementation
dart
@pragma("vm:shared")
static final operatingSystemVersion = _Platform.operatingSystemVersion;

packageConfig no setter

String? get packageConfig

The --packages flag passed to the executable used to run the script in this isolate.

If present, it specifies a file describing how Dart packages are looked up.

Is null if there is no --packages flag.

Implementation
dart
static String? get packageConfig => _Platform.packageConfig;

pathSeparator final

final String pathSeparator

The path separator used by the operating system to separate components in file paths.

Implementation
dart
@pragma("vm:platform-const")
static final pathSeparator = _Platform.pathSeparator;

resolvedExecutable no setter

String get resolvedExecutable

The path of the executable used to run the script in this isolate after it has been resolved by the OS.

This is the absolute path, with all symlinks resolved, to the executable used to run the script.

See executable for the unresolved version.

Implementation
dart
static String get resolvedExecutable => _Platform.resolvedExecutable;

script no setter

Uri get script

The absolute URI of the script being run in this isolate.

If the script argument on the command line is relative, it is resolved to an absolute URI before fetching the script, and that absolute URI is returned.

URI resolution only does string manipulation on the script path, and this may be different from the file system's path resolution behavior. For example, a symbolic link immediately followed by '..' will not be looked up.

If a compiled Dart script is being executed the URI to the compiled script is returned, for example, file:///full/path/to/script_name.exe.

If running on the Dart VM the URI to the running Dart script is returned, for example, file:///full/path/to/script_name.dart.

If the executable environment does not support script, the URI is empty.

Implementation
dart
static Uri get script => _Platform.script;

version final

final String version

The version of the current Dart runtime.

The value is a semantic versioning string representing the version of the current Dart runtime, possibly followed by whitespace and other version and build details.

Implementation
dart
@pragma("vm:shared")
static final version = _Platform.version;