UriData.fromString constructor

UriData.fromString(
  1. String content, {
  2. String? mimeType,
  3. Encoding? encoding,
  4. Map<String, String>? parameters,
  5. bool base64 = false,
})

Creates a data: URI containing the content string.

Equivalent to Uri.dataFromString(...).data, but may be more efficient if the uri itself isn't used.

Implementation

factory UriData.fromString(String content,
    {String? mimeType,
    Encoding? encoding,
    Map<String, String>? parameters,
    bool base64 = false}) {
  StringBuffer buffer = StringBuffer();
  List<int> indices = [_noScheme];
  String? charsetName = parameters?["charset"];
  String? encodingName;
  if (encoding == null) {
    if (charsetName != null) {
      encoding = Encoding.getByName(charsetName);
    }
  } else if (charsetName == null) {
    // Non-null only if parameters does not contain "charset".
    encodingName = encoding.name;
  }
  encoding ??= ascii;
  _writeUri(mimeType, encodingName, parameters, buffer, indices);
  indices.add(buffer.length);
  if (base64) {
    buffer.write(';base64,');
    indices.add(buffer.length - 1);
    buffer.write(encoding.fuse(_base64).encode(content));
  } else {
    buffer.write(',');
    _uriEncodeBytes(_uricTable, encoding.encode(content), buffer);
  }
  return UriData._(buffer.toString(), indices, null);
}