This is the 8th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
In dart: IO, provide API parity with the Platform class, but use instance properties instead of static properties. This difference allows for the use of these apis in testing, where you can provide mock implementations.
Take a look at the constructor:
class Platform {
static int get numberOfProcessors => _numberOfProcessors;
static String get pathSeparator => _pathSeparator;
static String get localeName => _Platform.localeName();
static String get operatingSystem => _operatingSystem;
static String get operatingSystemVersion => _operatingSystemVersion;
static String get localHostname => _localHostname;
static final bool isLinux = (_operatingSystem == "linux");
static final bool isMacOS = (_operatingSystem == "macos");
static final bool isWindows = (_operatingSystem == "windows");
static final bool isAndroid = (_operatingSystem == "android");
static final bool isIOS = (_operatingSystem == "ios");
static final bool isFuchsia = (_operatingSystem == "fuchsia");
static Map<String, String> get environment => _Platform.environment;
static String get executable => _Platform.executable;
static String get resolvedExecutable => _Platform.resolvedExecutable;
static Uri get script => _Platform.script;
static List<String> get executableArguments => _Platform.executableArguments;
static String get packageRoot => _Platform.packageRoot;
static String get packageConfig => _Platform.packageConfig;
static String get version => _version;
}
Copy the code
As you can see, there are many methods. Next, let’s introduce how to use them and what each attribute represents.
## How to use
### #1. Introduce the package first:
import ‘dart:io’;
2. Use:
IsAndroid platform. operatingSystem Use the same methods as above.
# # # 3, for example:
void _btnPress() {
print(Platform.isAndroid); // true/false
}
Copy the code
If your phone is Android, it will print true in the console, false otherwise.
The following is based on the Nexus5X API 28 test
attribute | type | Machine print result | describe |
---|---|---|---|
numberOfProcessors | int | 4 | The number of execution units of a machine. |
pathSeparator | String | / | The path separator used by the operating system to separate components in a file path. |
localeName | String | en_US | Gets the name of the current locale. |
operatingSystem | String | android | A string representing the operating system or platform. |
operatingSystemVersion | String | Linux 4.4.124+ #1 SMP PREEMPT Mon Jun 18 17:10:07 UTC 2018 | A string representing the operating system or platform version. |
localHostname | String | localhost | The local host name of the system |
isLinux | bool | false | Whether the operating system is [Linux] |
isMacOS | bool | false | Whether the operating system version is [macOS] |
isWindows | bool | false | Whether the version of the operating system is Windows |
isAndroid | bool | true | Whether the operating system version is [Android] |
isIOS | bool | false | Whether the operating system is [IOS] |
isFuchsia | bool | false | Whether the version of the operating system is Fuchsia |
environment | Map< String, String > | {PATH:/sbin:/system/sbin:/system/bin:/s } .. Such a series of strings is very long.. | The context for this process is a mapping from string keys to string values. The mapping is not modifiable, and its contents are retrieved from the operating system when first used. Environment variables on Windows are case insensitive, so on Windows, the mapping is case insensitive and all keys are converted to uppercase. On other platforms, keys can be case-sensitive. |
executable | String | /system/bin/app_process32 | The path to the executable file used to run the script in this isolation. The literal path used to identify the script. This path may be relative, or simply search the system path to find the name of the executable file. Use [resolvedExecutable] to obtain the absolute path of the executable file. |
resolvedExecutable | String | /system/bin/app_process32 | The path to the executable file used to run scripts in this isolation after the operating system resolves it. This is the absolute path to parse all symbolic links to the executable file used to run the script. |
script | Uri | file:///main.dart | The absolute URI of the script to run in this quarantine. If the executable environment does not support (scripting), the URI is empty |
executableArguments | List< String > | [] | In this quarantine, run the flags that the script passes to the executable. These are the command line flags of the executable that precede the name of the script. A new list is provided each time the values are read. |
packageRoot | String | null | --package-root Flag is passed to the executable to run the script in the quarantine. If there is no--package-root Flag, null |
packageConfig | String | null | --package Flag is passed to the executable to run the script in the quarantine. If there is no--package Flag, null |
version | String | (Sat May 26 03:16:14 2018 +0000) on “android_ia32” 2.0.0-dev.58.0. Flutter -f981f09760 (Sat May 26 03:16:14 2018 +0000) | Version of the current DART runtime. |