PlatformInterface class Null safety

Base class for platform interfaces.

Provides a static helper method for ensuring that platform interfaces are implemented using extends instead of implements.

Platform interface classes are expected to have a private static token object which will be be passed to verifyToken along with a platform interface object for verification.

Sample usage:

abstract class UrlLauncherPlatform extends PlatformInterface {
  UrlLauncherPlatform() : super(token: _token);

  static UrlLauncherPlatform _instance = MethodChannelUrlLauncher();

  static const Object _token = Object();

  static UrlLauncherPlatform get instance => _instance;

  /// Platform-specific plugins should set this with their own platform-specific
  /// class that extends [UrlLauncherPlatform] when they register themselves.
  static set instance(UrlLauncherPlatform instance) {
    PlatformInterface.verifyToken(instance, _token);
    _instance = instance;


Mockito mocks of platform interfaces will fail the verification, in test code only it is possible to include the MockPlatformInterfaceMixin for the verification to be temporarily disabled. See MockPlatformInterfaceMixin for a sample of using Mockito to mock a platform interface.



PlatformInterface({required Object token})
Pass a private, class-specific const Object() as the token.


hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toString() String
A string representation of this object. [...]


operator ==(Object other) bool
The equality operator. [...]

Static Methods

verifyToken(PlatformInterface instance, Object token) → void
Ensures that the platform instance has a token that matches the provided token and throws AssertionError if not. [...]