setFromAsset method

Future<void> setFromAsset({
  1. required String path,
  2. int? maxSize,
  3. dynamic onAdd()?,
})

Implementation

Future<void> setFromAsset({
  required final String path,
  final int? maxSize,
  final Function()? onAdd
}) async {
  if (path.isEmpty) throw Exception("path cant be empty");
  if (_urlPattern.hasMatch(path)) throw Exception("A URL can't be an asset");

  final extension = path.split('.').last.split('?').first;
  if(!_fileTypes.contains(extension)) {
    reset(error: false);
  } else {
    await rootBundle.load(path)
    .then((data) async {
      await ImageInterface().write(
        name      : '${DateTime.now().millisecondsSinceEpoch}${Random().nextInt(10000)}-${path.split('/').last.split('.').first}',
        extension : extension,
        bytes     : data.buffer.asUint8List(),
        maxSize   : maxSize
      ).then((_f) {
        _file       = _f;
        _bytes      = _f.bytes;
        _list       = _f.bytes == null ? [] : _f.bytes!.toList();
        _error      = false;
        _hasImage   = true;
        _extension  = extension;
        onAdd?.call();
        notifyListeners();
      }).onError((error, stackTrace) => reset(error: true));
    })
    .onError((error, stackTrace) => reset(error: true));
  }
}