decodeImageProvider static method
Decodes the given value
to an ImageProvider. This expects a specific
"type" attribute to be one of:
asset
memory
network
The expected structure depends on the exact "type" value passed in.
Type: asset
{
"assetName": <String>,
"package": <String>,
"type": "asset"
}
Type: memory
{
"bytes": <String>,
"scale": <double>,
"type": "memory"
}
Type: network
{
"headers": <Map<String, String>>,
"type": "network",
"scale": <double>,
"url": <String>
}
Implementation
static ImageProvider? decodeImageProvider(
dynamic value, {
bool validate = true,
}) {
ImageProvider? result;
if (value is ImageProvider) {
result = value;
} else {
assert(value == null || value['type'] is String);
_checkSupported(
'ImageProvider.type',
[
'asset',
'network',
'memory',
],
value == null ? null : value['type'],
);
if (value != null) {
assert(SchemaValidator.validate(
schemaId: '$_baseSchemaUrl/image_provider',
value: value,
validate: validate,
));
switch (value['type']) {
case 'asset':
result = AssetImage(
value['assetName'],
package: value['package'],
);
break;
case 'memory':
result = MemoryImage(
base64Decode(value['bytes']),
scale: JsonClass.parseDouble(value['scale'], 1.0)!,
);
break;
case 'network':
result = NetworkImage(
value['url'],
headers: value['headers'],
scale: JsonClass.parseDouble(value['scale'], 1.0)!,
);
break;
}
}
}
return result;
}