Skip to content

Use static final instead of static const for token used with plugin_platform_interface #937

@collinjackson

Description

@collinjackson

🐛 Bug Report

Describe the bug
This plugin should not use a const Object() as the platform interface token. There was an error in the documentation for plugin_platform_interface; see flutter/flutter#96178.

The consequence is that someone could develop an implementation of GeolocatorPlatform using implements instead of extends, and everything would work initially but changes to the platform interface would break that implementation.

To fix, use a static final Object _token = Object(); instead of static const Object _token = Object(); in GeolocatorPlatform.

Also, consider updating geolocator_platform_interface's dependency on plugin_platform_interface to ^2.1.1 and using PlatformInterface.verify instead of verifyExtends. This will prevent regressions.

/cc @stuartmorgan

Expected behavior

Should not be possible to build a class that implements GeolocatorPlatform using implements because GeolocatorPlatform uses a non-const token and calls verify.

Reproduction steps

  1. Build a class that implements GeolocatorPlatform instead of using extends. Use const Object() as the token. e.g.
class DontDoThis extends PlatformInterface implements GeolocatorPlatform {
  DontDoThis(): super(token: const Object());
  ...
}
...
GeolocatorPlatform.instance = DontDoThis();
  1. Make an app that depends on the new class and imports the file where it is defined.
  2. Make a minor change to GeolocatorPlatform (adding a method).
  3. Compile error "DontDoThis is missing implementations for these members ..."

Configuration

Version: geolocator 8.0.1. geolocator_platform_interface 3.0.1

Platform:
any

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions