This guide describes how errors and edge cases are represented when using the Zstandard plugin and CLI, and how to handle them in your code.
The main plugin and CLI return Future<Uint8List?> for compress and decompress. A null result means the operation failed (e.g. compression error, corrupted or invalid input for decompression).
Always check for null before using the result:
final compressed = await zstandard.compress(data, 3);
if (compressed == null) {
// Compression failed; log or show an error
return;
}
final decompressed = await zstandard.decompress(compressed);
if (decompressed == null) {
// Decompression failed; data may be corrupted or not zstd
return;
}- Calling
compress()ordecompress()on a nullUint8List?returns null (no throw). - If the underlying operation fails, the Future completes with null.
Uint8List? maybeData = ...;
final compressed = await maybeData.compress(); // null if maybeData is null
final decompressed = await compressed?.decompress(); // null if compressed is null or decompress fails- Decompression: Passing data that is not a valid Zstandard frame (e.g. random bytes, truncated data) typically results in a null return. The plugin does not throw in this case.
- Compression: Invalid compression level (e.g. out of range) may or may not be validated by the implementation; behavior can differ by platform. Use levels 1–22 for portability.
- UnimplementedError: Thrown by the default platform implementation (method channel) when
compressordecompressis called without a registered native implementation. In normal use with the full plugin and a supported platform, this should not occur. - MissingPluginException: Can occur if the method channel is used but no implementation is registered (e.g. in tests). Register a mock platform or the real implementation to avoid it.
- DynamicLibrary loading: On native platforms, if the zstd library fails to load, the first FFI call may throw. Ensure the app is built and run on a supported platform/architecture.
- Check null after every
compressanddecompresswhen failure is possible. - Use null-safe chains with extensions:
compressed?.decompress(). - Log or report null results in production (e.g. analytics, user message) instead of ignoring them.
- Validate input when it comes from untrusted sources (e.g. file upload); invalid data will usually yield null on decompress.