W3cubDocs

/Dart 2

Platform class

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 so on.

Get the URI to the current Dart script

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

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.

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, and isWindows.

import 'dart:io' show Platform, stdout;

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');
  }
}

Other resources

Dart by Example provides additional task-oriented code samples that show how to use various API from the dart:io library.

Constructors

Platform()

Properties

hashCodeint
read-only, inherited
The hash code for this object. [...]
runtimeTypeType
read-only, inherited
A representation of the runtime type of the object.

Methods

noSuchMethod(Invocation invocation) → dynamic
inherited
Invoked when a non-existent method or property is accessed. [...]
toString() → String
inherited
Returns a string representation of this object.

Operators

operator ==(dynamic other) → bool
inherited
The equality operator. [...]

Static Properties

environmentMap<String, String>
read-only
The environment for this process as a map from string key to string value. [...]
executableString
read-only
The path of the executable used to run the script in this isolate. [...]
executableArgumentsList<String>
read-only
The flags passed to the executable used to run the script in this isolate. [...]
isAndroidbool
final
Whether the operating system is a version of Android.
isFuchsiabool
final
Whether the operating system is a version of Fuchsia.
isIOSbool
final
Whether the operating system is a version of iOS.
isLinuxbool
final
Whether the operating system is a version of Linux. [...]
isMacOSbool
final
Whether the operating system is a version of macOS.
isWindowsbool
final
Whether the operating system is a version of Microsoft Windows.
localeNameString
read-only
Get the name of the current locale.
localHostnameString
read-only
The local hostname for the system.
numberOfProcessorsint
read-only
The number of individual execution units of the machine.
operatingSystemString
read-only
A string representing the operating system or platform.
operatingSystemVersionString
read-only
A string representing the version of the operating system or platform.
packageConfigString
read-only
The --packages flag passed to the executable used to run the script in this isolate. [...]
packageRootString
@Deprecated('packages/ directory resolution is not supported in Dart 2'), read-only
This returns null, as packages/ directories are no longer supported.
pathSeparatorString
read-only
The path separator used by the operating system to separate components in file paths.
resolvedExecutableString
read-only
The path of the executable used to run the script in this isolate after it has been resolved by the OS. [...]
scriptUri
read-only
The absolute URI of the script being run in this isolate. [...]
versionString
read-only
The version of the current Dart runtime. [...]

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-io/Platform-class.html