How to open file with a Flutter app on MacOS/Windows?

Viewed 1617

I have an app that should open certain file types. From the app itself, it's quite straightforward with file_picker plugin. But how to open a file trough the file manager? In other words, trough the "Open with" context menu.

I tried checking the arguments passed to main, but they are always empty. Registering a universal link wouldn't make much sense either since I only need to pass the file - nothing more.

1 Answers
if (await canLaunch("file://..path...xlsx")) {
  await launch("file://..path...xlsx");
} else {
  print("cannot launch url ]:");
}

this simple snippet basically works both on windows and mac os! If you can deal with path with classic path_provider, you'd be able to use the same feature on web, ios, android also. In my test, web downloaded the file when i typed and entered the file:// url.

So, i guess the uri scheme is the one who does these tricks. I tried a few types of files and as i commented, excel works just fine and the finder(or file explorer) also work. In my test, pptx even worked with the file url. Yes, it opens the native app. I couldn't have found out "open with.." option, but it meant that it automatically open xlsx file in excel(ie. ATM i don't have any option to launch "open with.." pop-up tho).

Similarly, i could have even found that instagram:// also works with url launcher. One thing unexpectedly tough was to set a proper path on mac os desktop. Since the app is being debugged somewhere assigned previously by Mr. Apple, simply calling methods from path_provider returned a path far away from desktop path, the home screen, since the app is running on somewhere else(like /Users/${MacOsUser}/Library/Containers/${fullPackagePath}/Data/Documents for mac os, which would be different in the distribution stage).

Hope this would help. FYI, i couldn't launch battle.net and league of legends with this method, which don't show their extensions in the file explorer ]: Have a wonderful day [:

Related