DataURLBase64.from constructor

DataURLBase64.from(
  1. Object payload, [
  2. Object? mimeType
])

Instantiates a DataURLBase64 automatically resolving payload and mimeType.

Implementation

factory DataURLBase64.from(Object payload, [Object? mimeType]) {
  String? m =
      mimeType is MimeType ? mimeType.toString() : mimeType?.toString();

  if (payload is List<int>) {
    return DataURLBase64(base64.encode(payload), m);
  } else if (payload is DataURLBase64) {
    return DataURLBase64(
        payload.payloadBase64, m ?? payload.mimeTypeAsString);
  } else {
    var s = payload.toString();

    var dataUrl = DataURLBase64.parse(s);
    if (dataUrl != null) {
      return DataURLBase64(
          dataUrl.payloadBase64, m ?? dataUrl.mimeTypeAsString);
    } else {
      var bs = _decodeBase64(s);
      if (bs != null) {
        return DataURLBase64(s, m);
      } else {
        return DataURLBase64(Base64.encode(s), m ?? 'text/plain');
      }
    }
  }
}