showFilePicker function

void showFilePicker({
  1. required dynamic action(
    1. List<File> file
    ),
  2. FileType fileType = FileType.image,
  3. bool allowMultiple = false,
  4. List<String>? allowedExtensions,
})

Implementation

void showFilePicker({
  required final Function(List<File> file) action,
  final FileType fileType = FileType.image,
  final bool allowMultiple = false,
  final List<String>? allowedExtensions,
}) async {
  final FilePickerResult? result = await FilePicker.platform.pickFiles(
    type: fileType,
    allowMultiple: allowMultiple,
    allowedExtensions: allowedExtensions,
  );
  if (result != null) {
    if (allowMultiple) {
      final List<File> files = <File>[];
      for (var i in result.files) {
        if (i.path != null) files.add(File(i.path!));
      }
    } else {
      final File file = File(result.files.single.path!);
      action(<File>[file]);
    }
  }
}