tree: 43f47a4d5edcf97c382957572634fcd9c8eb0866
  1. example/
  2. lib/
  3. out/
  4. test/
  5. tool/
  6. .gitignore
  7. analysis_options.yaml
  8. AUTHORS
  9. CHANGELOG.md
  10. LICENSE
  11. migration-guide.md
  12. pubspec.yaml
  13. README.md
pkgs/platform/README.md

Pub

A generic platform abstraction for Dart.

Cross-platform platform API

Provides access to platform specific information of the current runtime platform through a cross-platform API.

On a native platform, the NativePlatform class in this package provides a lightweight wrapper around the static Platform properties of dart:io, with the same rich, Dart-idiomatic API for querying the current runtime.

On the web, there is no NativePlatform object, and cross-platform client code can avoid dependencies on such properties simply by checking if Platform.current.nativePlatform is null. Instead web code has a Platform.current.browserPlatform values that is a non-null BrowserPlatform.

Example:

import 'package:platform/platform.dart';

void main() {
  switch (Platform.current) {
    case Platform(:var nativePlatform?):
      // `nativePlatform` has `dart:io`'s `Platform` properties.
      print(
        'Running on ${nativePlatform.operatingSystem} v.'
        '${nativePlatform.operatingSystemVersion}',
      );
    case Platform(:var browserPlatform?):
      // `browserPlatform` has version information from `windows.userAgent`.
      print('Running on ${browserPlatform.userAgent}');
  }
}

Code size

Using platform-specific properties like Platform.isLinux as branch conditions will allow a production compiler to completely omit the branch that it knows it's not currently compiling for.

Testing and Mocking

In tests, the package:platform/testing.dart library provides ways to mock platform objects with custom tailored configurations, and allows Zone based overriding of the Platform.current value.

[!NOTE] The testing library should only be used in tests, since its mere presence may affect how much code a compiled program will retain.