The platform interface defines the contract between the main zstandard plugin and each platform-specific implementation. It uses the plugin_platform_interface pattern for type-safe, testable platform abstraction.
All platform implementations must extend ZstandardPlatform and implement:
| Method | Signature | Description |
|---|---|---|
getPlatformVersion |
Future<String?> |
Returns a string identifying the platform (e.g. for debugging or display). |
compress |
Future<Uint8List?> compress(Uint8List data, int compressionLevel) |
Compresses data with the given level (1–22). Returns compressed bytes or null on failure. |
decompress |
Future<Uint8List?> decompress(Uint8List data) |
Decompresses zstd-compressed data. Returns decompressed bytes or null on failure. |
ZstandardPlatform extends PlatformInterface from plugin_platform_interface:
- Token: A unique token is used so that only valid platform instances can be set on
ZstandardPlatform.instance. - Default implementation:
MethodChannelZstandardPlatformis the default. It implements onlygetPlatformVersion()via the method channelplugins.flutter.io/zstandard.compress()anddecompress()are not implemented and throwUnimplementedErrorif called on the default implementation. - Registration: Each platform package calls
ZstandardPlatform.instance = MyPlatform()(with the correct token) in itsregisterWith()(or equivalent) so the main plugin uses the real implementation.
sequenceDiagram
participant App
participant Main as zstandard
participant Impl as ZstandardImpl
participant PM as PlatformManager
participant Reg as Platform registerWith
participant PI as ZstandardPlatform.instance
App->>Main: Zstandard().compress(data, 3)
Main->>Impl: instance
Impl->>PM: isAndroid / isIOS / ...
PM-->>Impl: platform
Impl->>Reg: ZstandardAndroid.registerWith() (e.g.)
Reg->>PI: instance = ZstandardAndroid()
Impl->>PI: return instance
Main->>PI: compress(data, 3)
PI-->>Main: Uint8List?
- First call to
Zstandard().instancetriggers registration. ZstandardImplchecksPlatformManagerand calls the appropriateregisterWith().- That sets
ZstandardPlatform.instanceto the concrete implementation. - The main plugin then forwards
compress/decompress/getPlatformVersionto that instance.
- Single API: Applications use only the main package; they do not reference platform packages directly.
- Testability: Tests can replace
ZstandardPlatform.instancewith a mock that implements the same three methods. - Federated packages: Each platform lives in its own package with its own native build and dependencies.
- Web vs native: The main package uses conditional imports to select native or web
ZstandardImpl; both paths end up with a registeredZstandardPlatformimplementation.
- Create a new package (e.g.
zstandard_fuchsia) that depends onzstandard_platform_interface. - Implement a class that extends
ZstandardPlatformand implementsgetPlatformVersion,compress, anddecompress. - Expose a
registerWith()(or similar) that setsZstandardPlatform.instance = YourPlatform()using the token from the interface package. - In the main plugin’s
ZstandardImpl, add detection for the new platform and call yourregisterWith()when that platform is active.