|
1 | | -[](https://pub.dev/packages/platform) |
| 1 | +[](https://pub.dartlang.org/packages/platform) |
2 | 2 |
|
3 | 3 | A generic platform abstraction for Dart. |
4 | 4 |
|
5 | | -### Cross-platform platform API |
6 | | -Provides access to platform specific information of the current runtime platform |
7 | | -through a cross-platform API. |
| 5 | +Like `dart:io`, `package:platform` supplies a rich, Dart-idiomatic API for |
| 6 | +accessing platform-specific information. |
8 | 7 |
|
9 | | -On a native platform, the `NativePlatform` class in this package provides a |
10 | | -lightweight wrapper around the static `Platform` properties of `dart:io`, |
11 | | -with the same rich, Dart-idiomatic API for querying the current runtime. |
12 | | - |
13 | | -On the web, there is no `NativePlatform` object, and cross-platform client code |
14 | | -can avoid dependencies on such properties simply by checking if |
15 | | -`Platform.current.nativePlatform` is `null`. |
16 | | -Instead web code has a `Platform.current.browserPlatform` values that is |
17 | | -a non-`null` `BrowserPlatform`. |
18 | | - |
19 | | -Example: |
20 | | -```dart |
21 | | -import 'package:platform/platform.dart'; |
22 | | -
|
23 | | -void main() { |
24 | | - switch (Platform.current) { |
25 | | - case Platform(:var nativePlatform?): |
26 | | - // `nativePlatform` has `dart:io`'s `Platform` properties. |
27 | | - print( |
28 | | - 'Running on ${nativePlatform.operatingSystem} v.' |
29 | | - '${nativePlatform.operatingSystemVersion}', |
30 | | - ); |
31 | | - case Platform(:var browserPlatform?): |
32 | | - // `browserPlatform` has version information from `windows.userAgent`. |
33 | | - print('Running on ${browserPlatform.userAgent}'); |
34 | | - } |
35 | | -} |
36 | | -``` |
37 | | - |
38 | | -### Code size |
39 | | - |
40 | | -Using platform-specific properties like `Platform.isLinux` as branch conditions |
41 | | -will allow a production compiler to completely omit the branch |
42 | | -that it knows it's not currently compiling for. |
43 | | - |
44 | | -### Testing and Mocking |
45 | | - |
46 | | -In tests, the `package:platform/testing.dart` library provides ways to mock |
47 | | -platform objects with custom tailored configurations, |
48 | | -and allows `Zone` based overriding of the `Platform.current` value. |
49 | | -> [!NOTE] |
50 | | -> The testing library should only be used in tests, since its mere presence |
51 | | -> may affect how much code a compiled program will retain. |
| 8 | +`package:platform` provides a lightweight wrapper around the static `Platform` |
| 9 | +properties that exist in `dart:io`. However, it uses instance properties rather |
| 10 | +than static properties, making it possible to mock out in tests. |
0 commit comments