flutterdartfilepicker

Flutter: A problem when using FilePicker to pick files


Some of my codes is as following:

File? _file;
  PlatformFile? _platformFile;

  selectFile() async {
    final file = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['mp3', 'm4a']
    );

    if (file != null) {
      setState(() {
        _file = File(file.files.single.path!);
        _platformFile = file.files.first;
        print("Size: ");
        print(_platformFile?.size);
      });
    }

    loadingController.forward();
  }

When calling selectFile, there should be a list full of .mp3 and .m4a files. However, there is no files shown in the list. The picture is as following.enter image description here

I wondered that if it is beacuse that some varibles have not be set? Or some other details I have not noticed?

Thanks for your help.


Solution

  • According to File Picker your code seems to be okay but I don't know why your gallery showing empty.

    As alternative solution is that you can remove allowedExtensions option and use FileType.any so that any file can be picked.

    After picked a file, you can manually check the picked file extension and if the extension doesn't match mp3, m4a then show/throw an error otherwise go forward.