How to import multiple Dart files in the same folder - dart

As the question is self-explaining, assume in the folder I have multiple Dart file. Instead of importing file by file (for example import 'screens/screen_1.dart'; import 'screens/screen_2.dart';...), I want to batch import (such as import 'screens/*.dart') but it doesn't work.
Do you know Dart allows us to do such batch import?

Dart imports don't support importing multiple files at once.
What you can do is creating a library that exports other libraries
screens.dart
export 'screens/screen_1.dart';
export 'screens/screen_2.dart';
export 'screens/screen_3.dart';
foo.dart
import 'screens.dart';

Related

Should I use `package:` import when importing my own package's library in Dart?

I'm little bit confused about which style of import should I use when importing implementation libraries (code under src/lib) from another libraries (and tests) in my own package?
The docs recommends the opposite options.
In pub docs, it says:
When you use libraries from within your own package, even code in src, you can (and should) still use package: to import them.
But in "Effective Dart", it says:
When referencing a library inside your package’s lib directory from another library in that same package, use a relative URI, not an explicit package: URI.
In the end, which style should I use in such cases:
import implementation library (under lib/src/) from with public library (under just lib/)?
import implementation library (lib/src/foo.dart) from within its test (test/src(?)/foo_test.dart)?
import public library (lib/foo.dart) from within its test (test/foo_test.dart)?

Dart style recommendation on importing local file

Is there any recommendation how to import local files. In my case I would have 2 options:
import 'package:workshop/feed/item.dart';
import 'item.dart';
I haven't found anything on that on the guide Effective Dart: Style nor on Google search.
There are at least two different recommendations, depending on who you ask.
Either works, I personally recommend the latter, shorter, variant.
It is sufficient and it avoids the issue of hard-coding your package name into every file. If you ever want to rename the package, that will be a drudge.
There is one issue which makes some people recommend the former format.
If you import a package library using a non-package: URI, say by having a file in the bin/ or test/ directory do an import like:
import "../lib/mylib.dart";
then that library is now imported using two different URIs:
package:mypkg/mylib.dart and
file:///somewhere/mypkg/lib/mylib.dart
Since Dart identifies libraries by their import URIs, these two imports will be treated as different libraries, each with their own global and static variables, which just happen to have the same source code. That's an annoying problem, and can be hard to debug. If you use the long package:... import everywhere, then at least the issue is restricted to the first library you import. If you use a relative import, import "src/helper.dart";, then that library will now also exist in two versions:
package:mypkg/src/helper.dart and
file:///somewhere/mypkg/lib/src/helper.dart
The real issue here was the first import which contained a /lib/ in the path. You must never have such an import. Using package: URIs for all imports may reduce the issue, but not remove it.
I recommend using the relative path, and making sure that you never have a /lib/ in any import path. Libraries in the /lib/ directory of a pub package are package libraries and should be referred to using package: URIs. If you do that, then relative URIs will be resolved against the package URI and again be a package URI, and all is well.

How to import package 'Storage' in to Swift project?

I'm currently studying the source code of Firefox-iOS app by creating my own Swift project and typing the code line by line.
In one of the source code files, it imported a packaged named Storage
But I don't think the package Storage is part of the apple API and I don't really know how I can import it.
Edit
Multiple podfiles are present in the project folder
In Swift you dont import other Swift files as they are readily available to use directly.
But you need to import another module. It looks like Storage here is a module inside the firefox-ios app workspace and hence you need to import it before using it.
I looked at the sourcecode at Github and it does contain a package named Storage.
You can read this to understand more about Modules and import statement.

How can I import all files in a folder?

Say I got a bunch of dart scripts in a folder,
Is there anything I can do like import 'foo/*.dart'?
P.S. What if I got an array of filenames and wanna import those files?
You need to import each library individually.
What you can do is to create a library that imports all other libraries and reexports them.
you can then import this one library and get all libraries imported at once.
library all_in_one;
export library1.dart;
export library2.dart;
export library3.dart;
You could also use the "part of" library composition:
Create 1 .dart file that is your lib eg.: lib.dart and add at the start of this file.:
library lib
For every file in your folder add a:
part "somefile.dart"
part "otherfile.dart"
In all files that are part of this library add at the start:
part of lib
In other files and libs you can then import all those file just via:
import "lib.dart"
This will import all the parts of your library (folder). Keep in mind that the "lib.dart" file is now responsible for all the imports of your lib files. So to import something to "somefile.dart" you add the import to "lib.dart". All imports are then available in all the lib files.
This would be highly unsecure, this is why it is not allowed by design. An attacker would be able to run any malicious code just adding a file with the right name to your folder.

Jena package does not exist

Hi all I have a java program inside it I have imported these classes.
import com.hp.hpl.mesa.rdf.jena.mem.ModelMem;
import com.hp.hpl.mesa.rdf.jena.model.*;
import com.hp.hpl.mesa.rdf.jena.common.PropertyImpl;
when I'm trying to compile the program it shows 3 errors which are:
package com.hp.hpl.mesa.rdf.jena.mem does not exist import
com.hp.hpl.mesa.rdf.jena.mem.ModelMem;
package com.hp.hpl.mesa.rdf.jena.model does not exist import
com.hp.hpl.mesa.rdf.jena.model.*;
package com.hp.hpl.mesa.rdf.jena.common does not exist import
com.hp.hpl.mesa.rdf.jena.common.PropertyImpl;
I have tried to search for the jar files but I couldn't, is anyone has the link to download the jar file of those packages.
The Jena packages are under com.hp.hpl.jena.rdf…. I don't know where you got this com.hp.hpl.mesa.rdf.jena… package name from, but it's definitely wrong. Maybe you're using an obsolete example from an ancient Jena version?
There shouldn't be any reason to import ModelMem or PropertyImpl directly. If you need a ModelMem, use ModelFactory.createDefaultModel(). If you need a property instance, use model.createProperty().
You can download the Jena jars from the Jena download site.

Resources