Uri.dataFromBytes constructor Null safety

  1. List<int> bytes,
  2. {String mimeType = "application/octet-stream",
  3. Map<String, String>? parameters,
  4. bool percentEncoded = false}

Creates a data: URI containing an encoding of bytes.

Defaults to Base64 encoding the bytes, but if percentEncoded is true, the bytes will instead be percent encoded (any non-ASCII or non-valid-ASCII-character byte is replaced by a percent encoding).

To read the bytes back, use UriData.contentAsBytes.

It defaults to having the mime-type application/octet-stream. The mimeType and parameters are added to the created URI. If any of these contain characters that are not allowed in the data URI, the character is percent-escaped. If the character is non-ASCII, it is first UTF-8 encoded and then the bytes are percent encoded.


final uri = Uri.dataFromBytes([68, 97, 114, 116]);
print(uri); // data:application/octet-stream;base64,RGFydA==


factory Uri.dataFromBytes(List<int> bytes,
    {String mimeType = "application/octet-stream",
    Map<String, String>? parameters,
    bool percentEncoded = false}) {
  UriData data = UriData.fromBytes(bytes,
      mimeType: mimeType,
      parameters: parameters,
      percentEncoded: percentEncoded);
  return data.uri;