Flutter conditionnal import mobile vs desktop

249

There is no way to do what you are describing; conditional imports only support library-based conditions, and there is no difference in supported libraries for mobile and desktop.

Share:
249
Lulupointu
Author by

Lulupointu

Updated on December 28, 2022

Comments

  • Lulupointu
    Lulupointu over 1 year

    Importing on web vs native is easy

    import '../wrappers/platform/platform_none.dart'
        if (dart.library.io) '../wrappers/platform/platform_io.dart'
        if (dart.library.js) '../wrappers/platform/platform_web.dart';
    

    What if I want to import a different package depending on whether I am on mobile (iOS/Android) vs on desktop (Linux/MacOS/Windows)?

    If there is no way with conditional import, how can you achieve that any other way?

  • Lulupointu
    Lulupointu about 3 years
    Theorically the dart team could create dummy libraries that would serve this purpose then.
  • Lulupointu
    Lulupointu over 2 years
    Anyway sorry for not accepting earlier after much research you are right it cannot be done with the shipped libraries. I still think it's a pain but anyway ;)
  • Valentin Vignal
    Valentin Vignal over 2 years
    @Lulupointu have you found a workaround to this?
  • Lulupointu
    Lulupointu over 2 years
    @ValentinVignal As I said in one of the comment, one solution for me (having only an Android version of the plugin) was to create a dummy version of the class I needed manually (which only returns error) and then use conditional import. That's not the best but at least it work if you really need to, no matter how tedious