- Dart Cookbook
- Ivo Balbaert
- 403字
- 2021-08-05 17:42:46
Getting information from the operating system
In this recipe, you will see how to interact with the underlying operating system on which your app runs by making system calls and getting information from the system.
Getting ready
The Platform
class provides you with information about the OS and the computer the app is executing on. It lives in dart:io
, so we need to import this library.
How to do it...
The following script shows the use of some interesting options (refer to the code files tools
\code
\platform
\bin
\platform.dart
of this chapter):
import 'dart:io'; Map env = Platform.environment; void main() { print('We run from this VM: ${Platform.executable}'); // getting the OS and Dart version: print('Our OS is: ${Platform.operatingSystem}'); print('We are running Dart version: ${Platform.version}'); if (!Platform.isLinux) { print('We are not running on Linux here!'); } // getting the number of processors: int noProcs = Platform.numberOfProcessors; print('no of processors: $noProcs'); // getting the value of environment variables from the Map env: print('OS = ${env["OS"]}'); print('HOMEDRIVE = ${env["HOMEDRIVE"]}'); print('USERNAME = ${env["USERNAME"]}'); print('PATH = ${env["PATH"]}'); // getting the path to the executing Dart script: var path = Platform.script.path; print('We execute at $path'); // on this OS we use this path separator: print('path separator: ${Platform.pathSeparator}'); }
When run, the above code gives the following output:
Our OS is: windows We are running Dart version: 1.3.3 (Wed Apr 16 12:40:55 2014) on "windows_ia32" We are not running on Linux here! no of processors: 8 OS = Windows_NT HOMEDRIVE = C: USERNAME = CVO PATH = C:\mongodb\bin;C:\MinGW\bin;... We execute at /F:/Dartiverse/platform/bin/platform.dart path separator: \
How it works...
Most of the options are straightforward. You can get the running VM from Platform.executable
. You can get the OS from Platform.operatingSystem
; this can also be tested on a Boolean property such as Platform.isLinux
. The Dart version can be tested with the Platform.version
property. The Platform.environment
option returns a nice map structure for the environment variables of your system, so you can access their values by name, for example, for a variable envVar
, use var envVar = Platform.environment["envVar"]
.
To get the path of the executing Dart script, you can use the path property of Platform.script
because the latter returns the absolute URI of the script. When building file paths in your app, you need to know how the components in a path are separated; Platform.pathSeparator
gives you this information.
There's more...
Don't confuse this class with Platform
from dart:html
, which returns information about the browser platform.
- FreeSWITCH 1.2
- 微商之道
- 光網絡評估及案例分析
- 物聯網概論(第2版)
- Learning Karaf Cellar
- 信息通信網絡建設安全管理概要2
- Building RESTful Web Services with Spring 5(Second Edition)
- Learning Swift(Second Edition)
- 通信原理及MATLAB/Simulink仿真
- Scala Design Patterns.
- 6G無線網絡空口關鍵技術
- Getting Started with nopCommerce
- Building RESTful Web Services with .NET Core
- 人際網絡
- Intelligent Mobile Projects with TensorFlow