encodeImageProvider static method

Map<String, dynamic>? encodeImageProvider(
  1. ImageProvider<Object>? value, {
  2. bool validate = true,
})

Encodes the given value to an JSON value. 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>,
  "type": "asset",
  "package": <String>
}

Type: memory

{
  "bytes": <String>,
  "type": "memory",
  "scale": <double>
}

Type: network

{
  "headers": <Map<String, String>>,
  "type": "network"
  "scale": <double>,
  "url": <String>
}

Implementation

static Map<String, dynamic>? encodeImageProvider(
  ImageProvider? value, {
  bool validate = true,
}) {
  Map<String, dynamic>? result;

  if (value != null) {
    assert(
        value is AssetImage || value is MemoryImage || value is NetworkImage);
    if (value is AssetImage) {
      result = <String, dynamic>{
        'assetName': value.assetName,
        'package': value.package,
        'type': 'asset',
      };
    } else if (value is MemoryImage) {
      result = <String, dynamic>{
        'bytes': base64Encode(value.bytes),
        'scale': value.scale,
        'type': 'memory',
      };
    } else if (value is NetworkImage) {
      result = <String, dynamic>{
        'headers': value.headers,
        'scale': value.scale,
        'type': 'network',
        'url': value.url,
      };
    }
  }

  return result;
}