The zstandard package adds extension methods on Uint8List? so you can call compress and decompress directly on byte data.
import 'package:zstandard/zstandard.dart';The extensions are exported from the main library.
Extension on nullable Uint8List. If the receiver is null, both methods return null without calling the platform.
Future<Uint8List?> compress({int compressionLevel = 3})Compresses this byte list using the default (or specified) compression level.
- compressionLevel: Optional; defaults to 3. Range 1–22.
- Returns: Compressed bytes, or
nullif the receiver isnullor compression failed.
Example:
final data = Uint8List.fromList([10, 20, 30, 40, 50]);
final compressed = await data.compress();
final compressedHigh = await data.compress(compressionLevel: 10);Future<Uint8List?> decompress()Decompresses this byte list, which must be Zstandard-compressed data.
- Returns: Decompressed bytes, or
nullif the receiver isnullor decompression failed.
Example:
final compressed = await data.compress(compressionLevel: 3);
final decompressed = await compressed?.decompress();- On
nullreceiver,compress()anddecompress()returnnulland do not throw. - Always check the result for
nullwhen the source might be null or when the operation can fail.
Example:
Uint8List? maybeData = ...;
final compressed = await maybeData.compress();
if (compressed != null) {
final back = await compressed.decompress();
}- Main API —
Zstandardclass - Usage Examples