showMaterialFilePicker function

Future<void> showMaterialFilePicker({
  1. BuildContext? context,
  2. FileType fileType = FileType.any,
  3. List<String>? allowedExtensions,
  4. ValueChanged<PlatformFile>? onChanged,
})

Allows selection of a file.

Implementation

Future<void> showMaterialFilePicker({
  BuildContext? context,

  /// The type of the file to retrieve (filter)
  FileType fileType = FileType.any,

  /// What allowed extensions to look for
  List<String>? allowedExtensions,

  /// Function that gets called when the value is changed
  ValueChanged<PlatformFile>? onChanged,
}) async {
  FilePickerResult? result = await FilePicker.platform.pickFiles(
    type: fileType,
    withData: true,
    allowMultiple: false,
    allowedExtensions: allowedExtensions,
  );
  if (result != null && result.files.length == 1) {
    PlatformFile file = result.files.single;
    if (onChanged != null) onChanged(file);
  }
}