16

I have flutter application which uses different webview plugin for each platform(mobile, web, window).
Though I am able to import platform base on web and mobile, I am not able to import for windows.

I have tried adding else condition if it is not mobile or web, but it is taking mobile plugin.

  1. This is how I import package for web and mobile (Working).
import 'package:eam_flutter/form/mobileui.dart'
    if (dart.library.html) 'package:eam_flutter/form/webui.dart'
    as multiPlatform;   
  1. This is how I import package for web, mobile and windows (Not Working, it is showing mobile webview exception since it doesn't support desktop).
import 'package:eam_flutter/form/windowui.dart'
    if (dart.library.html) 'package:eam_flutter/form/webui.dart'
    if (dart.library.io) 'package:eam_flutter/form/mobileui.dart'
    as multiPlatform;

How can I specify conditional imports for windows?

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
KonTash
  • 158
  • 1
  • 2
  • 15
  • 1
    You cannot; see https://stackoverflow.com/questions/62724798/conditional-package-import-for-desktop-in-flutter/67948233#67948233 – smorgan Jun 12 '21 at 11:09
  • @smorgan is there any workaround to this? – Kunchok Tashi Jun 14 '21 at 06:18
  • @codingwithtashi Workaround for what? This question doesn't state a problem to be solved, only a desired solution, and that particular solution is impossible. There are probably other solutions to the underlying problem. – smorgan Jun 14 '21 at 10:10
  • @smorgan I meant, how to make it work without platform error, but never mind I got it working. Thanks – Kunchok Tashi Jun 14 '21 at 12:33
  • I just did the code above and am having a breakpoint here: completer.complete(files.map((html.File file) – user40823 Dec 26 '22 at 22:28

5 Answers5

16

For anyone else finding this, note that the accepted answer is not an answer to the question that was asked. The answer to the question that was asked is that you cannot. There is no way to use conditional imports to get different behavior between mobile and desktop; see this comment from the Dart team.

smorgan
  • 20,228
  • 3
  • 47
  • 55
  • How to use the same code for mobile/web if no conditional imports are available? – Nazarii Kahaniak Oct 18 '22 at 09:55
  • @NazariiKahaniak You're asking a different question. You *can* have conditional imports based on VM vs. web (as demonstrated in the question); you cannot have conditional imports based on different VM platforms (e.g. mobile vs. desktop). – jamesdlin Feb 26 '23 at 09:46
5

Since there is no conditional import support for window since it comes under dart io. I have this workaround and found it working.
I end up creating file for each platform with different package import.

import 'package:flutter/foundation.dart' show kIsWeb;
import 'dart:io' as io;
if(kIsWeb){
{
     return WebPage();  //your web page with web package import in it
}
else if (!kIsWeb && io.Platform.isWindows) {
     return WindowsPage(); //your window page with window package import in it
    } 
else if(!kIsWeb && io.Platform.isAndroid) {
     return AndroidPage();  //your android page with android package import in it
    } 
//you can add others condition...
Kunchok Tashi
  • 2,413
  • 1
  • 22
  • 30
  • 5
    Note that this is an answer to a different question than was asked. The question was about having different *compile-time* behavior; this gives different *runtime* behavior. For cases where you want the latter this isn't really a workaround, it's the normal way of doing it. For the former (if for some reason that is actually needed), it doesn't help so isn't a workaround at all. – smorgan Jun 14 '21 at 13:46
  • @smorgan, thanks. With given codes I can avoid the issue which I am getting. I think I should go with this since there is no conditional import for windows – KonTash Jun 14 '21 at 14:21
  • 5
    @KonTash This is why it's usually much better to ask questions about the *problem* you are trying to solve, rather than how to implement one specific solution that you think is the solution to the problem. If you'd asked how to run different code on different platforms, this would be a good answer answer. Instead there's now a SO question with an incorrect accepted answer that will likely confused other people in the future. – smorgan Jun 14 '21 at 17:59
1

Maybe we no longer need conditional import. Look at the code below:

import 'package:package1/package1.dart';
import 'package:package2/package2.dart';

const keepFunc1 = bool.fromEnvironment('KEEP_FUNC1');
dynamic result2;

void main() {
  if (keepFunc1) {
    result2 = Calculator1()..addOne(1);
  } else {
    result2 = Calculator2()..addOne(1);
  }
  runApp(const MyApp());
}

If KEEP_FUNC1 environment variable is not specified to true. The package1 and the class Caculator1 won't be packaged into apk or ipa.

For more details, see the answer I wrote here.

So we can import all packages and use a const environment value to decide what packages to import. The tree-shaking mechanism is smart enough to remove unused parts.

yancaico
  • 1,145
  • 11
  • 21
1

Instead use that package.

universal_html
Mustafa Ekici
  • 7,263
  • 9
  • 55
  • 75
0

Check this example you need to create 2 files one for web & another for os and use condition on import

  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 20:08