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';
Related
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
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';
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.
I'm building a Speech to text app but when I try to import the library speech_to_text, I'm facing to that message unused import package:speech_to_text/speech_to_text.dart;
it means that its not used yet. continue creating the code and when you use the tts, the warning will go away.
I'm using Quasar and the PWA starter kit, with VueJS.
I want to know how to declare once a set of components used throughout many pages in a Vue app.
I seem to need to have a huge import declaration in every page
I tried to import once in main.js but there is a conflicting
import Quasar from 'quasar'
so I can't add to that or change it to
import {
Quasar,
QCard,
.... etc
} from 'quasar'
as the {} seems to break the import.
Also can't import twice (duplicate import error).
What is the way to import components once for a whole app?
Or get at the Vue object so it can be used in another way to import inside a different component?
You can import specific components to register globally in the main.js file when you register the Quasar plugin.
For example, in you want to be able to use the <q-btn> and <q-icon> components anywhere in your application you could do this:
import Quasar, { QBtn, QIcon } from 'quasar'
Vue.use(Quasar, {
components: { QBtn, QIcon }
})
Here's the documentation.