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 verify along with a platform interface object for verification.

Sample usage:

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

  static UrlLauncherPlatform _instance = MethodChannelUrlLauncher();

  static final 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.verify(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.

Implementers

Constructors

PlatformInterface({required Object token})
Constructs a PlatformInterface, for use only in constructors of abstract derived classes. [...]

Properties

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

Methods

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

Operators

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

Static Methods

verify(PlatformInterface instance, Object token) → void
Ensures that the platform instance was constructed with a non-const token that matches the provided token and throws AssertionError if not. [...]
verifyToken(PlatformInterface instance, Object token) → void
Performs the same checks as verify but without throwing an AssertionError if const Object() is used as the instance token. [...]