loadBuffer method

  1. @override
Future<ImmutableBuffer> loadBuffer(
  1. String key
)
override

Retrieve a binary resource from the asset bundle as an immutable buffer.

Throws an exception if the asset is not found.

Implementation

@override
Future<ui.ImmutableBuffer> loadBuffer(String key) async {
  if (kIsWeb) {
    final ByteData bytes = await load(key);
    return ui.ImmutableBuffer.fromUint8List(Uint8List.sublistView(bytes));
  }
  bool debugUsePlatformChannel = false;
  assert(() {
    // dart:io is safe to use here since we early return for web
    // above. If that code is changed, this needs to be guarded on
    // web presence. Override how assets are loaded in tests so that
    // the old loader behavior that allows tests to load assets from
    // the current package using the package prefix.
    if (Platform.environment.containsKey('UNIT_TEST_ASSETS')) {
      debugUsePlatformChannel = true;
    }
    return true;
  }());
  if (debugUsePlatformChannel) {
    final ByteData bytes = await load(key);
    return ui.ImmutableBuffer.fromUint8List(Uint8List.sublistView(bytes));
  }
  try {
    return await ui.ImmutableBuffer.fromAsset(key);
  } on Exception catch (e) {
    throw FlutterError.fromParts(<DiagnosticsNode>[
      _errorSummaryWithKey(key),
      ErrorDescription(e.toString()),
    ]);
  }
}