Search code examples
ioslistflutterassetsimagepicker

HOW to convert List<File> into List<Asset> in flutter


I am using multiple image picker to get mutliple images from gallery and it return a list of Asset, and i have successfully converted that image to list of files as List by using path from asset.

resultList = await MultiImagePicker.pickImages(
    maxImages: 5,
    enableCamera: false,
  );

file.add(asset.identifier);

var path = await FlutterAbsolutePath.getAbsolutePath(resultList[i].identifier);
                _pathofimages.add(File(path));

but not i need to edit that again as i have implemented crop feature as well... so in that case i need to reverse the process

like adding asset in List using filepath


Solution

  • I made a converter function. Would you try using this?

    uuid: ^2.1.0

    import 'package:path/path.dart';
    import 'package:uuid/uuid.dart';
    
    ...
    
      Future<Asset> fileToAsset(File image) async {
        String fileName = basename(image.path);
        var decodedImage = await decodeImageFromList(image.readAsBytesSync());
        return Asset(uuid.v4(), fileName, decodedImage.width, decodedImage.height);
      }