skip to Main Content

I have two profiles in my phone, personal and work profile. The file picker I am using works well in the personal profile, but when I try to use the same file picker in the work profile, I get the error mentioned below.

[MethodChannelFilePicker] Platform exception: PlatformException(already_active, File picker is already active, null, null)
E/flutter (16388): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(already_active, File picker is already active, null, null)
E/flutter (16388): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
E/flutter (16388): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)
E/flutter (16388): <asynchronous suspension>
E/flutter (16388): #2      MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:496:35)
E/flutter (16388): <asynchronous suspension>
E/flutter (16388): #3      FilePickerIO._getPath (package:file_picker/src/file_picker_io.dart:92:33)
E/flutter (16388): <asynchronous suspension>
E/flutter (16388): #4      _UploadFileWidgetState.selectFile (package:feeta/views/other_widgets/upload_file_widget.dart:40:32)
E/flutter (16388): <asynchronous suspension>

This is the code of FilePicker I am using:

Future<void> selectFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['pdf', 'jpg', 'jpeg', 'png'],
      allowMultiple: false,
    );

    if (result != null && result.files.isNotEmpty) {
      File selected = File(result.files.single.path!);
      String fileExtension = selected.path.split('.').last.toLowerCase();

      if (fileExtension == 'pdf' || fileExtension == 'jpg' ||
          fileExtension == 'jpeg' || fileExtension == 'png') {
        int fileSize = selected.lengthSync();
        if (fileSize <= 1024 * 1024) {
          if (widget.dialogType == "postLeave") {
            _allLeaveViewModel.selectedFile.value = selected;
            print("SelectedFile1: ${_allLeaveViewModel.selectedFile.value}");
          } else {
            _myLeavesViewModel.selectedFile.value = selected;
            print("SelectedFile2: ${_myLeavesViewModel.selectedFile.value}");
          }
          setState(() {
            selectedFile = selected;
          });
        } else {
          showFileSizeExceededDialog();
        }
      } else {
        showInvalidFileTypeDialog();
      }
    }
  }

This is the dependency I am using:

  cupertino_icons: ^1.0.2
  http: ^1.0.0
  #webview_flutter: ^4.2.2
  flutter_inappwebview: ^5.7.2
  #flutter_html: ^3.0.0-beta.2
  flutter_secure_storage: ^8.0.0
  dio: ^5.3.2
  jwt_decoder: ^2.0.1
  file_picker: ^5.3.3

Do I need to handle this filePicker differently for the work profile?
Can anyone please tell me why I am getting this problem only in the work profile, where the same code is working fine in the personal profile.
Any help or suggestion would be appreciated!.

2

Answers


  1. Updated: file_picker: ^5.3.3 is working flawlessly in the below-written code.

    Here’s the solution. I tried to implement the same functionality as yours. But the only difference is I’m using the latest version of the File Picker package. I used 5.5.0, which is the new version available for the Picker.

    Use file_picker: ^5.5.0 from link: https://pub.dev/packages/file_picker

    Source code:

      Future<void> selectFile() async {
        final FilePickerResult result = await FilePicker.platform.pickFiles(
              type: FileType.custom,
              allowedExtensions: <String>["pdf", "jpg", "jpeg", "png"],
            ) ??
            const FilePickerResult(<PlatformFile>[]);
            
        if (result.files.isNotEmpty) {
          final File selected = File(result.files.single.path ?? "");
          final String ext = selected.path.split(".").last.toLowerCase();
          if (ext == "pdf" || ext == "jpg" || ext == "jpeg" || ext == "png") {
            final int size = await selected.length();
            if (size <= 1024 * 1024) {
              // Your Code Goes Here
            } else {
              log("Size Exceeded");
            }
          } else {
            log("Invalid File Type");
          }
        } else {
          log("Nothing Picked From File Picker");
        }
        return Future<void>.value();
      }
    
    Login or Signup to reply.
  2. In addition to the above answer try to stop the app and follow these steps:

    1. Run the command Flutter clean
    2. Run the command Flutter pub get
    3. Then run the app

    This works fine with me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search