decompressText static method

  1. @useResult
String? decompressText(
  1. String? compressedBase64
)

Decompresses a Base64-encoded gzipped string back to its original form.

This method reverses the compressText operation by:

  1. Decoding the Base64 string to bytes
  2. Decompressing the gzipped bytes
  3. Decoding the UTF-8 bytes back to a string

Returns null if:

  • The input compressedBase64 is 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:

  • compressText to 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;
  }
}