Flutter: import package only for web users - flutter

I see that Flutter has conditional import statements, but after looking at some examples I’m still confused.
If I want to have main.dart import package “package:xyz/xyz.dart” only when the user is on web, how can I achieve that in the simplest way? Thanks for any tips.

We could import a package only for the web users by the use of a conditional import:
import 'package:xyz/mobile.dart'
if (dart.library.html) 'package:xyz/web.dart';
The code above imports package:xyz/web.dart only if dart.library.html is available, which happens to be the case for the web platform.

Related

How to add conditional imports across Flutter mobile,web and window?

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.
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;
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?
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.
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...
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.
Check this example
you need to create 2 files one for web & another for os and use condition on import

How to import flutter (not dart) collections library?

I'm able to import
import 'dart:collection';
But that's not the same as the library with an API here:
https://api.flutter.dev/flutter/package-collection_collection/package-collection_collection-library.html
I can't seem to figure out the correct uri for import this library, and can't find any thing on the web or the api about this.
import 'package:collection/collection.dart';

Condition import of dart:io or dart:html

I need to use the File class which is different (as for what I understand) in web and html
I would like to be able to do
import if (dart.library.io) 'dart:io' if (dart.library.io) 'dart:html';
but this doesn't work.
What is the solution to reading File from dart:io on mobile apps and from dart:html in web?
Thank you
The cross_file package can help you. Or you can use conditional imports like :
import 'dart:io' if (dart.library.html) 'dart:html';

Flutter conditionnal import mobile vs desktop

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?
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.

How do you import the ML Kit Object Detection library in Xcode?

As per this link, I'm able to install the podfile just fine: https://developers.google.com/ml-kit/vision/object-detection/ios#objc
https://i.stack.imgur.com/AqkFZ.png
But what's the actual import statement at the top of my code? I can't find that anywhere. For example I'm trying to find something like the following code example but can't anywhere.
import GoogleMLKit
import GoogleMLKitObjectDetection
I must be missing something very simple.
In the example projects on Github the implmentation is
import MLKit
Here are differents example projects about its use. The link is in the documentation you shared.
https://github.com/googlesamples/mlkit/tree/master/ios/quickstarts/vision