loadImages method

Future loadImages(
  1. String path,
  2. int maxCount
)

To load photos from device. @param path:- path of file from where needs to show image. Default path :- Camera. @param maxCount:- Maximum number of photos will return.

Implementation

Future loadImages(String path, int maxCount) async {
  var path = await FilePicker.platform.getDirectoryPath();
  var root = Directory(path ?? '$path/DCIM/Camera');

  await root.exists().then((isExist) async {
    int maxImage = maxCount;
    var listImage = blankList();
    if (isExist) {
      FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['jpeg', 'png', 'jpg'],
      );

      List<File> files =
          result!.paths.map((path) => File(path ?? '')).toList();
      debugPrint('file length---> ${files.length}');

      /// [file] by default will return old images.
      /// for getting latest max number of photos [file.sublist(file.length - maxImage, file.length)]

      List<File> filesList = files.length > maxImage
          ? files.sublist(files.length - (maxImage + 1), files.length - 1)
          : files;

      for (int i = 0; i < filesList.length; i++) {
        listImage[i].imageUrl = File(filesList[i].path);
      }
    }

    add(ImageListEvent(listImage));
  });
}