load method Null safety

  1. @override
Future<ByteData> load(
  1. String key
)
override

Retrieve a binary resource from the asset bundle as a data stream.

Throws an exception if the asset is not found.

Implementation

@override
Future<ByteData> load(String key) {
  final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull(key)).path);
  final Future<ByteData>? future = ServicesBinding.instance.defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData())?.then((ByteData? asset) {
    if (asset == null) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        _errorSummaryWithKey(key),
        ErrorDescription('The asset does not exist or has empty data.'),
      ]);
    }
    return asset;
  });
  if (future == null) {
    throw FlutterError.fromParts(<DiagnosticsNode>[
        _errorSummaryWithKey(key),
        ErrorDescription('The asset does not exist or has empty data.'),
      ]);
  }
  return future;
}