compressText static method

String? compressText(
  1. String? value
)

Compresses a string using gzip and encodes it as Base64.

This method first encodes the input string to UTF-8, then compresses it using gzip, and finally encodes the compressed bytes as a Base64 string.

Returns null if:

  • The input value is empty
  • An error occurs during compression

Example:

final compressed = Base64Utils.compressText('Hello, World!');
// compressed is a Base64-encoded gzipped string

See also:

Implementation

static String? compressText(String? value) {
  if (value == null || value.isEmpty) {
    return null;
  }

  try {
    final List<int> encodedJson = utf8.encode(value);
    final List<int> gzipJson = io.gzip.encode(encodedJson);

    return base64.encode(gzipJson);
  } on Exception {
    return null;
  }
}