Sub topic 16
Splitting a Plugin into a Federated Package Set
This chapter assumes you can write Dart and Flutter plugins, understand MethodChannel, and have basic Kotlin and Swift knowledge. It focuses on package structure and dependency flow rather than native method-channel implementation.
A federated plugin splits one feature into several packages. The packages share one public Dart API, while each platform has its own implementation package. This structure is useful when different teams maintain platforms, when a plugin supports many platforms, or when platform implementations need different native dependencies.
The package roles
A typical federated plugin contains these packages:
| Package | Main responsibility | Usually published |
|---|---|---|
| App-facing package | Public Dart API and platform interface dependency | Yes |
| Platform interface | Abstract contract that implementations must follow | Yes |
| Android implementation | Kotlin or Java code for Android | Yes |
| iOS implementation | Swift or Objective-C code for iOS | Yes |
The app-facing package is the package that application developers add to pubspec.yaml. It should hide platform-specific details. The platform interface defines the operations and data types that every implementation must provide.
An implementation package depends on the platform interface package. The app-facing package depends on the platform interface and normally declares the platform implementations as default implementations.
flowchart TD
App[Flutter application] --> Core[weather package]
Core --> Interface[weather_platform_interface]
Core --> Android[weather_android]
Core --> IOS[weather_ios]
Android --> Interface
IOS --> InterfaceDefine the platform interface
The interface package contains an abstract Dart class. It should not contain Android or iOS code.
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
abstract class WeatherPlatform extends PlatformInterface {
WeatherPlatform() : super(token: _token);
static final Object _token = Object();
static WeatherPlatform _instance = _DefaultWeatherPlatform();
static WeatherPlatform get instance => _instance;
static set instance(WeatherPlatform implementation) {
PlatformInterface.verify(implementation, _token);
_instance = implementation;
}
Future<double> temperature(String city) {
throw UnimplementedError('temperature() has not been implemented.');
}
}
class _DefaultWeatherPlatform extends WeatherPlatform {}PlatformInterface helps prevent an implementation from accidentally bypassing the intended interface contract. The token check also makes future interface changes safer for third-party implementations.
Register implementations
The app-facing package exposes a simple API and selects an implementation during package initialization.
import 'package:weather_platform_interface/weather_platform_interface.dart';
class Weather {
static Future<double> temperature(String city) {
return WeatherPlatform.instance.temperature(city);
}
}In practice, the Android and iOS packages register themselves through Flutter's plugin mechanism. The app-facing package's pubspec.yaml identifies the native classes and their platform packages:
flutter:
plugin:
platforms:
android:
default_package: weather_android
ios:
default_package: weather_iosThe platform implementation package then declares its native plugin class:
flutter:
plugin:
implements: weather
platforms:
android:
package: com.example.weather
pluginClass: WeatherPluginThe implements value connects the implementation to the app-facing plugin. default_package tells Flutter which package to use when an application has not selected another implementation.
Choose federated structure deliberately
Federation adds packages, releases, and dependency constraints. Use it when the separation provides a real benefit.
A single plugin package is often simpler for a small project with one maintainer. A federated structure is a better fit when implementations evolve independently or external contributors maintain platforms.
Test the contract
Test the app-facing API with a mock implementation of the platform interface. Test each native package separately on its target platform. This keeps Dart contract tests fast while leaving device behavior to platform integration tests.
Exercise
Why should an app-facing package depend on a platform interface instead of importing Android or iOS code directly?
Show answer
The interface gives the app-facing Dart API one stable contract while allowing each platform implementation to change independently.