UriData.fromString 构造函数

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

创建一个包含 content 字符串的 data: URI。

Uri.dataFromString(...).data 等效,但如果无需使用该 uri,则可能更高效。

实现

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);
}