-1

This seems like an issue that won't last for long, but I'm looking for a final solution for opening up a native file picker on all platforms including Desktop without installing go-flutter.

Currently I'm using file_selector plugin by Flutter.dev and it's working for web and macOS (I have yet to test on windows/linux). When I try to run it on an iOS simulator, I get an error.

MissingPluginException(No implementation found for method openFile on channel plugins.flutter.io/file_selector)

I guess this is because it doesn't support iOS and maybe this plugin is just for desktop and web.

I also saw the file_picker plugin but it requires go-flutter to be installed on the desktop to get that to work. I'd rather have nice hot-reloads/builds directly in Android studio and not need go-flutter commands to run the desktop simulations.

Is there a one stop solution that basically combines these two things? I'm pretty new to Flutter, so maybe I'm missing a really easy way to use one or the other library depending on platform with some kind of conditional? It seems kind of messy to go that route, but it could work for now.

The other answer that I'm looking for is a good solution to using file_picker plugin for desktop in an easier way, such as how to use go-flutter seamlessly.

Art C
  • 842
  • 2
  • 10
  • 21
  • For your go-flutter-desktop questions, keep in mind that using it isn't just a matter of tooling differences. It's a *totally different embedding*. Applications you build with it will not behave the same way for end users than allocations built with the official desktop embeddings. You'll see bugs there that don't exist in the official embeddings, for instance. You also wouldnt be able to use any plugins other than go-flutter-desktop plugins. – smorgan Aug 12 '21 at 12:06
  • @smorgan thank you, that makes sense. I'll avoid this go-flutter-desktop option for sure. – Art C Aug 12 '21 at 18:11

5 Answers5

1

I ended up using a conditional to detect the Platform and will use file_picker for iOS/Android and then file_selector for web/desktop.

So far it's working well. I'll probably write a quick wrapper to abstract that out of the main code.

Art C
  • 842
  • 2
  • 10
  • 21
1

The current latest version of the file_picker plugin (4.4.0) claims to work on all supported platforms, incl. desktop and Web.

I trie with Flutter 2.10.2 on macOS 12.2.1 and it works well. Also successfully tested on Win10.

final path = await FilePicker.platform.saveFile(
  dialogTitle: 'Expor data',
  fileName: '_${bloc.lot}_${bloc.serial}.dat',
  lockParentWindow: true,
);

Setting lockParentWindow makes the file picker application modale, btw.

Ber
  • 40,356
  • 16
  • 72
  • 88
0

It seems like the easiest way to do this currently is to use a package like file_picker_desktop for Windows/Mac/Linux and file_picker on Android/iOS/Web. They seem to have similar APIs, so you can make a wrapper function on your app that calls the correct library.

caiopo
  • 454
  • 2
  • 8
  • 1
    `file_selector`, which the OP is already using, will provide substantially better UX than `file_picker_desktop`. The latter spawns separate processes that show windows that aren't connected to the application in any way. They will have focus issues, interact badly with application switching, etc. – smorgan Aug 12 '21 at 11:56
0
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;


if (Platform.isAndroid || Platform.isIOS)) {
 // use file_picker plugin
      FilePickerResult? result = await FilePicker.platform.pickFiles();
     if(result != null) {
     File file = File(result.files.single.path);
     } else {
     // User canceled the picker
     }
} else if(kIsWeb) { 
  // use file_selector plugin 
 final typeGroup = XTypeGroup(label: 'images', extensions: ['jpg', 
  'png']);
   final file = await openFile(acceptedTypeGroups: [typeGroup]);
} else {
// use file_picker_desktop
   try {
   final result = await pickFiles(
   allowMultiple: false,
   );
    if (result != null) {
    File file = File(result.files.single.path);
    } else {
    // User canceled the picker
    }
    } catch (e) {
    print(e);
    }
}
david okoroafor
  • 313
  • 1
  • 7
  • file_selector plugin actually now works for desktop as well. I'm now using file_picker for mobile apps, and everything else (including web) uses file_selector. – Art C Aug 12 '21 at 23:49
0

I just checked the site of file_selector and it says to support the following platforms now:

https://pub.dev/packages/file_selector

enter image description here

However, there is no Android support (as of now) as it seems.

Tim Brückner
  • 1,928
  • 2
  • 16
  • 27