miguelpruivo/flutter_file_picker

Dismissing file picker modal on iOS with drag down gesture can crash the app

Closed this issue · 2 comments

Describe the bug
There is an issue with dismissing File picker modal on iOS with drag down gesture. When opening file picker multiple times the app can crash.

Platform

  • Android
  • iOS
  • Web
  • Desktop

Platform OS version
16.1

How are you picking?

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('file_picker issue')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                  onPressed: () {
                    try {
                      FilePicker.platform.pickFiles();
                    } on PlatformException catch (_) {}
                  },
                  child: const Text('pick file')),
            ],
          ),
        ),
      ),
    );
  }

Details to reproduce the issue
Using the snippet above I'm quickly opening File picker and closing it using drag down gesture to dismiss modal (issue is not happening if I'm using Cancel button). After few times I start to get Platform Exceptions:

MethodChannelFilePicker] Platform exception: PlatformException(multiple_request, Cancelled by a second request, null, null)

This is fine and can be treated as expected. But after few more tries the app crashes.

Error Log
Crash log:

    frame #0: 0x0000000103828840 file_picker`-[FilePickerPlugin documentPickerWasCancelled:](self=0x0000000282cf1450, _cmd="documentPickerWasCancelled:", controller=0x000000013a205170) at FilePickerPlugin.m:614:5
   611 	#ifdef PICKER_DOCUMENT
   612 	- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
   613 	    Log(@"FilePicker canceled");
-> 614 	    _result(nil);
    	    ^
   615 	    _result = nil;
   616 	    [controller dismissViewControllerAnimated:YES completion:NULL];
   617 	}
Target 0: (Runner) stopped.

Screenshots and/or video
Exception caught in Xcode:
Screenshot 2023-01-05 at 09 22 54

Additional context
It seems that in delegate method _result can be null. Probably it is an easy fix in FilePickerPlugin.m:

#ifdef PICKER_AUDIO
- (void)mediaPickerDidCancel:(MPMediaPickerController *)controller {
    Log(@"FilePicker canceled");
    if(_result != nil) {
        _result(nil);
        _result = nil;
    }
    [controller dismissViewControllerAnimated:YES completion:NULL];
}
#endif // PICKER_AUDIO

#ifdef PICKER_DOCUMENT
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
    Log(@"FilePicker canceled");
    if(_result != nil) {
        _result(nil);
        _result = nil;
    }
    [controller dismissViewControllerAnimated:YES completion:NULL];
}
#endif // PICKER_DOCUMENT

#ifdef PICKER_MEDIA
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    Log(@"FilePicker canceled");
    if(_result != nil) {
        _result(nil);
        _result = nil;
    }
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
#endif

This issue is stale because it has been open for 14 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.