How can I import all files in a folder? - dart

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.

Related

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 multiple Dart files in the same folder

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';

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.

Install SwiftKeychainWrapper manually

I would like to install SwiftKeychainWrapper manually because I get error when I try to install it using CocoaPods.
In description it says that I need to download and drop KeychainWrapper.swift and KeychainItemAcessibility.swift into my project.
I clicked on Clone or download -> Download ZIP. In downloaded SwiftKeychainWrapper folder there are KeychainWrapper.swift and KeychainItemAcessibility.swift files. I drag and dropped them to my project under the same root where there are Main.storyboard, AppDelegate.swift,...
Then is says to use the keychain wrapper in your app, import SwiftKeychainWrapper into the file(s) where you want to use it.
I added import SwiftKeychainWrapper to a file where I want to use it and I get error No such module 'SwiftKeychainWrapper'.
What am I doing wrong, why can't I use it?
Just as #dan said you don't need to import any other modules if you add source files directly to your project. When a library is added via cocoapods, all of it's sources are in a separate module and you need to do import ModuleName to make them accessible.
If your read carefully github instructions, you'll find that for manual approach you should only copy the files and nothing more:
Manually
Download and drop KeychainWrapper.swift and
KeychainItemAcessibility.swift into your project.
P.S. Also make sure that you copy those files into you project folder before dragging them into Xcode because that will just add links to files in your Downloads folder or wherever they are placed.

Xcode / Objective C - Dynamically import files (via folder, regex, etc.)

I am creating an iOS framework (like an acyclic MVC) and want a Controller to be able to import all View files. The view files are all in a folder and have a *View.h naming.
Is it possible to import all views in one line by doing something like #import "/Views" or #import "*View.h"? What's the simplest way to dynamically import?
Currently I am importing a header file which contains the other header files. This works, but I would like a way to dynamically import so I can add a View and not have to update imports before using it.
I don't think you can do that because import is done during compile time. But you can archive the result by a script in which it generates the umbrella header contains all the #import "*View.h". Add it into Build Phases before compile sources

Resources