platform 3.2.0 copy "platform: ^3.2.0" to clipboard
platform: ^3.2.0 copied to clipboard

A pluggable, mockable platform information abstraction for Dart.

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.

120
likes
160
points
8.7M
downloads

Documentation

API reference

Publisher

verified publisherdart.dev

Weekly Downloads

A pluggable, mockable platform information abstraction for Dart.

Repository (GitHub)
View/report issues
Contributing

Topics

#information #platform

License

BSD-3-Clause (license)

Dependencies

meta

More

Packages that depend on platform