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.
Static Properties
- environment → Map<String, String>
-
Get the environment for this process.…
read-only - executable → String
-
Returns the path of the executable used to run the script in this isolate.…
read-only - executableArguments → List<String>
-
Returns the flags passed to the executable used to run the script in this isolate. These are the command-line flags between the executable name and the script name. Each fetch of executableArguments returns a new List, containing the flags passed to the executable.
read-only - isAndroid → bool
-
Returns true if the operating system is Android.
read-only - isIOS → bool
-
Returns true if the operating system is iOS.
read-only - isLinux → bool
-
Returns true if the operating system is Linux.
read-only - isMacOS → bool
-
Returns true if the operating system is OS X.
read-only - isWindows → bool
-
Returns true if the operating system is Windows.
read-only - localHostname → String
-
Get the local hostname for the system.
read-only - numberOfProcessors → int
-
Get the number of processors of the machine.
read-only - operatingSystem → String
-
Get a string (
linux
,macos
,windows
,android
, orios
) representing the operating system.read-only - packageConfig → String
-
Returns the value of the
--packages
flag passed to the executable used to run the script in this isolate. This is the configuration which specifies how Dart packages are looked up.…read-only - packageRoot → String
-
Returns the value of the
--package-root
flag passed to the executable used to run the script in this isolate. This is the directory in which Dart packages are looked up.…read-only - pathSeparator → String
-
Get the path separator used by the operating system to separate components in file paths.
read-only - resolvedExecutable → String
-
Returns the path of the executable used to run the script in this isolate after it has been resolved by the OS.…
read-only - script → Uri
-
Returns the absolute URI of the script being run in this isolate.…
read-only - version → String
-
Returns the version of the current Dart runtime.…
read-only
Constructors
- Platform()
Properties
- hashCode → int
-
Get a hash code for this object.…
read-only, inherited - runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Operators
-
operator ==(
other) → bool -
The equality operator.…
inherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.…
inherited -
toString(
) → String -
Returns a string representation of this object.
inherited