|
| 1 | +[](https://github.com/dart-lang/core/actions/workflows/os_detect.yaml) |
| 2 | +[](https://pub.dev/packages/os_detect) |
| 3 | +[](https://pub.dev/packages/os_detect/publisher) |
| 4 | + |
| 5 | +Platform independent access to information about the current operating system. |
| 6 | + |
| 7 | +## Querying the current OS |
| 8 | + |
| 9 | +Exposes `operatingSystem` and `operatingSystemVersion` strings similar to those |
| 10 | +of the `Platform` class in `dart:io`, but also works on the web. The |
| 11 | +`operatingSystem` of a browser is the string "browser". Also exposes convenience |
| 12 | +getters like `isLinux`, `isAndroid` and `isBrowser` based on the |
| 13 | +`operatingSystem` string. |
| 14 | + |
| 15 | +To use this package instead of `dart:io`, replace the import of `dart:io` with: |
| 16 | + |
| 17 | +```dart |
| 18 | +import 'package:os_detect/os_detect.dart' as os_detect; |
| 19 | +``` |
| 20 | + |
| 21 | +That should keep the code working if the only functionality used from `dart:io` |
| 22 | +is operating system detection. You should then use your IDE to rename the import |
| 23 | +prefix from `Platform` to something lower-cased which follows the style guide |
| 24 | +for import prefixes. |
| 25 | + |
| 26 | +Any new platform which supports neither `dart:io` nor `dart:html` can make |
| 27 | +itself recognizable by configuring the `dart.os.name` and `dart.os.version` |
| 28 | +environment settings, so that `const String.fromEnvironment` can access them. |
| 29 | + |
| 30 | +## Overriding the current OS string |
| 31 | + |
| 32 | +It's possible to override the current operating system string, as exposed by |
| 33 | +`operatingSystem` and `operatingSystemVersion` in |
| 34 | +`package:os_detect/os_detect.dart`. To do so, import the |
| 35 | +`package:os_detect/override.dart` library and use the `overrideOperatingSystem` |
| 36 | +function to run code in a zone where the operating system and version values are |
| 37 | +set to whatever values are desired. |
| 38 | + |
| 39 | +The class `OperatingSystemID` can also be used directly to abstract over the |
| 40 | +operating system name and version. The `OperatingSystemID.current` defaults to |
| 41 | +the values provided by the platform when not overridden using |
| 42 | +`overrideOperatingSystem`. |
0 commit comments