0

I am using package multi_image_picker2 to select multiple images. Below is my code.

Future<void> loadAssets() async {
    List<Asset> resultList = <Asset>[];
    String error = 'No Error Detected';

    try {
      resultList = await MultiImagePicker.pickImages(
        maxImages: 5,
        enableCamera: true,
        selectedAssets: images,
        cupertinoOptions: CupertinoOptions(
          takePhotoIcon: "chat",
          doneButtonTitle: "Fatto",
        ),
        materialOptions: MaterialOptions(
          actionBarColor: "#abcdef",
          actionBarTitle: "Pick Your Images",
          allViewTitle: "All Photos",
          useDetailsView: false,
          //selectCircleStrokeColor: "#000000",
        ),
      );
    } on Exception catch (e) {
      error = e.toString();
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;


    setState(() {
      images = resultList;
      //_error = error;
    });
  }

I need to convert these to File objects, so I can upload them to Amazon S3. But how can I convert this Asset object from multi_image_picker2 to a File object?

PeakGen
  • 21,894
  • 86
  • 261
  • 463

1 Answers1

0

You can simply use flutter_absolute_path:

 List<File> allImages = [];

 resultList.forEach((imageAsset) async {
        await FlutterAbsolutePath.getAbsolutePath(imageAsset.identifier)
            .then((filePath) {
          File tempFile = File(filePath);
          if (tempFile.existsSync()) {
           allImages.add(tempFile);
          }
        });
      });
Jitendra Mistry
  • 342
  • 1
  • 11