copyAssetToLocalStorage method

  1. @override
Future<void> copyAssetToLocalStorage(
  1. String assetName,
  2. String targetName
)
override

Copies the asset assetName to targetName on the local file system.

Implementation

@override
Future<void> copyAssetToLocalStorage(
    String assetName, String targetName) async {
  final resolved = await _resolve(targetName);
  // `rootBundle.load` throws a FlutterError for a missing asset, which is NOT
  // what a caller of this plugin is told to catch: every native platform
  // reports through a method channel, so the documented handler is
  // `on PlatformException`. Letting the FlutterError through meant that on
  // Windows and Linux the README's own example never caught the failure and
  // it escaped as an unhandled error. Same contract on every platform.
  final ByteData data;
  try {
    data = await assetLoader('assets/$assetName');
  } catch (e) {
    throw PlatformException(
      code: 'ERROR',
      message: 'Failed to copy asset "$assetName": $e',
    );
  }
  final bytes = data.buffer.asUint8List(
    data.offsetInBytes,
    data.lengthInBytes,
  );
  await File(resolved).writeAsBytes(bytes);
}