putString method
UploadTask
putString(
- String data, {
- PutStringFormat format = PutStringFormat.raw,
- SettableMetadata? metadata,
Upload a String value as a storage object.
Use PutStringFormat to correctly encode the string:
- PutStringFormat.raw the string will be encoded in a Base64 format.
- PutStringFormat.dataUrl the string must be in a data url format
(e.g. "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="). If no
SettableMetadata.mimeType
is provided as part of themetadata
argument, themimeType
will be automatically set. - PutStringFormat.base64 will be encoded as a Base64 string.
- PutStringFormat.base64Url will be encoded as a Base64 string safe URL.
Implementation
UploadTask putString(
String data, {
PutStringFormat format = PutStringFormat.raw,
SettableMetadata? metadata,
}) {
String _data = data;
PutStringFormat _format = format;
SettableMetadata? _metadata = metadata;
// Convert any raw string values into a Base64 format
if (format == PutStringFormat.raw) {
_data = base64.encode(utf8.encode(_data));
_format = PutStringFormat.base64;
}
// Convert a data_url into a Base64 format
if (format == PutStringFormat.dataUrl) {
_format = PutStringFormat.base64;
UriData uri = UriData.fromUri(Uri.parse(data));
assert(uri.isBase64);
_data = uri.contentText;
if (_metadata == null && uri.mimeType.isNotEmpty) {
_metadata = SettableMetadata(
contentType: uri.mimeType,
);
}
// If the data_url contains a mime-type & the user has not provided it,
// set it
if ((_metadata!.contentType == null || _metadata.contentType!.isEmpty) &&
uri.mimeType.isNotEmpty) {
_metadata = SettableMetadata(
cacheControl: metadata!.cacheControl,
contentDisposition: metadata.contentDisposition,
contentEncoding: metadata.contentEncoding,
contentLanguage: metadata.contentLanguage,
contentType: uri.mimeType,
);
}
}
return UploadTask._(
storage, _delegate.putString(_data, _format, _metadata));
}