decode static method

List<int> decode(
  1. String data, {
  2. bool validatePadding = true,
  3. bool urlSafe = true,
})

Decodes a Base64 data string into bytes.

validatePadding: If true (default), requires input length to be a multiple of 4. If false, padding '=' is added automatically to fix length.

urlSafe: If true (default), treats input as URL-safe Base64, converting '-' to '+' and '_' to '/' before decoding. If false, throws if URL-safe characters are present.

Throws B64ConverterException on invalid input or length.

Implementation

static List<int> decode(String data,
    {bool validatePadding = true, bool urlSafe = true}) {
  if (validatePadding && data.length % 4 != 0) {
    throw B64ConverterException("Invalid length, must be multiple of four");
  } else if (!validatePadding) {
    while (data.length % 4 != 0) {
      data += '=';
    }
  }
  if (urlSafe) {
    data = data.replaceAll('-', '+').replaceAll('_', '/');
  } else if (data.contains('-') || data.contains('_')) {
    throw B64ConverterException(
        'Invalid character in standard Base64 string: found URL-safe characters "-" or "_" but urlSafe is false.');
  }
  final encoder = _Base64StreamDecoder();
  try {
    encoder.add(data);
    return encoder.finalize().clone();
  } finally {
    encoder.clean();
  }
}