decompressText static method
Decompresses a Base64-encoded gzipped string back to its original form.
This method reverses the compressText operation by:
- Decoding the Base64 string to bytes
- Decompressing the gzipped bytes
- Decoding the UTF-8 bytes back to a string
Returns null if:
- The input
compressedBase64is empty - The input is not valid Base64
- The decoded data is not valid gzip
- Gzip is unavailable on the current platform (e.g., web)
- An error occurs during decompression
Example:
final original = Base64Utils.decompressText(compressedString);
See also:
compressTextto create compressed strings
Implementation
@useResult
static String? decompressText(String? compressedBase64) {
if (compressedBase64 == null || compressedBase64.isEmpty) {
return null;
}
try {
final Uint8List decodedBase64 = base64.decode(compressedBase64);
final List<int>? decodedGzip = gzipDecode(decodedBase64);
if (decodedGzip == null) {
return null;
}
return utf8.decode(decodedGzip);
} on FormatException catch (e, stackTrace) {
// debugPrint is appropriate for utility packages (stripped in release builds, no external dependencies)
// ignore: saropa_lints/avoid_print_error -- intentional diagnostic logging via debugPrint (stripped in release)
debugPrint('Base64Utils.decompressText failed: $e\n$stackTrace');
return null;
}
}