bytesFromDataUri function Null safety

Uint8List? bytesFromDataUri(
  1. String dataUri
)

Returns Uint8List by decoding dataUri.

Supported encoding:

  • base64
  • utf8

Implementation

Uint8List? bytesFromDataUri(String dataUri) {
  final match = _dataUriRegExp.matchAsPrefix(dataUri);
  if (match == null) {
    return null;
  }

  final prefix = match[0]!;
  final encoding = match[1];
  final data = dataUri.substring(prefix.length);
  final bytes = encoding == 'base64'
      ? base64.decode(data)
      : encoding == 'utf8'
          ? Uint8List.fromList(data.codeUnits)
          : null;

  return bytes?.isNotEmpty == true ? bytes : null;
}