Importing files under the src directory from a dart library - dart

Effective dart says
"DON’T import libraries that are inside the src directory of another package."
reason being it breaks abstraction and could potentially break your app if the library was to change its underlying implementation.
They do not provide an alternative or a solution.
Im currently working on a dart package that my app depends on.
What would be the correct way to import the models or classes from it instead of importing it directly from package src folder?

As previously mentioned in the comments, you can define which classes you can expose from your package. You're correct that it's recommended to avoid exposing more API than intended - it's also mentioned in the docs. To do this, you can define the exposed classes:
export 'src/your_file.dart' show ExposedClass hide HiddenClass;

Related

Using open source while writing a library in Swift

I'm trying to find more information / explanation for the following scenario:
I'm writing a library in Swift and would like to use some open source library in it.
If I just integrate them into my library, is there a chance of namespace collision?
What would happen if the host app will use:
The exact same open source library
The same library but different version
Does using CocoaPods changes something here?
Consider a scenario where I import AFNetworking for example (via CocoaPods) in my library, and the host app will use it too.
Using the same library, you won't have any issues. Using different version will likely cause problems, but that is going to be dependent on the changes made in the different versions.
Namespace collisions in Swift are rare. As #mattt stated, each module acts as a namespace, so naming conflicts with classes or functions from another module won't exist as they do in Objective-C. If you have a naming conflict, the compiler will tell you. In that case, you can just prefix the conflicting signature with the module name.
I would highly recommend you use Cocoapods for dependency management. It handles version control and will make your life much easier.

When to use part/part of versus import/export in Dart?

I do not completely understand the difference between part/part of and import/export when using libraries in Dart. For example:
one.dart:
library one;
part "two.dart";
Class One {
};
and
two.dart:
part of one;
import 'somefile.dart';
Class Two {
}
versus
library one;
import 'two.dart';
Class One {
}
and
library two;
import 'somefile.dart';
export 'somefile.dart';
Class Two {
}
Both scenarios seem to do the same thing. When is it advantageous to use part and part of rather than import? And are there scenarios where import will not work, but part and part of will?
update 2018/03
part and part of is used more and more for code generation scenarios recently (instead of deprecated transformers) and unlikely to go away anytime soon.
Packages like built_value, json_serializable, and many others depend on it.
Discouraged is only the patter where all files of a package are tied together to a single library by having one library file and all other files being part of that library.
original
In Dart, private members are accessible within the same library. With import you import a library and can access only its public members. With part/part of you can split one library into several files and private members are accessible for all code within these files.
see clarifications to below paragraph in above update
Using part / part of is discouraged and the Dart team is considering getting rid of it. I assume they will introduce something like "friend" (https://github.com/dart-lang/sdk/issues/22841), where two libraries can access each other's private members as an alternative before they discontinue part / part of (maybe in a future Dart version).
Let's suppose we have a Dart library called mylib, whose file is lib/mylib.dart.
library mylib;
// Definitions
That library can be included in the main.dart file as
import 'package:mypackage/mylib.dart';
When you create a new library and use other libraries you want to make available automatically when using your package, then you use export:
library mylib;
export 'otherlib.dart';
// Definitions
You can use the show keyword to import/export only some parts of a library (like a class or something).
You are using the part of directive wrong here. You can't use both library and part of, which is used to specify the contents that belong to a library. For example, you can split your library file in more than one file (the parts):
Suppose we have in the file mylib.dart:
library mylib;
part 'src/class1.part';
// More parts
And then we have in another file src/class1.part the part specified in mylib.dart
part of mylib;
class Class1 {
/* ... */
}
The Creating Library Packages article on the dartlang.org site recommends avoiding part / part of.
Note: You may have heard of the part directive, which allows you to
split a library into multiple Dart files. We recommend that you avoid
using part and create mini libraries instead.
The 'mini libraries' referred to are small library dart files in src which are imported into and exported from main libraries.
using part/part of makes many files be treated as if they were one file
import/export doesn't, so this may be useful when private fields need to be accessed from another files (classes created on other files)

Combining different dart files when executed by JS

I'm building up some Dart code that I would like to use in an app where it is essentially a library to the javascript. I'm wondering how I can specify which Dart files I'd like in the project to be part of the library. For example, theres Foo.dart and Bar.dart. How can I have the created product include both Foo.dart and Bar.dart in one file? I'm also concerned about tree shaking since none of the classes are instantiated in Dart.
There's also a Baz.dart, and I would like to have a different build for compiling Foo.dart and Baz.dart into a single file (though this is less important, as I can accomplish this would separate projects and some symlinking).
Thanks!
This use case (build a JavaScript library with Dart) isn't supported yet.
The reworked js-interop package is supposed to allow to do that but I don't know about it's current state.

Asynchronous module definition (AMD) with Angular.dart?

I am curious to know if its possible to have Asynchronous module definition with Angular.dart (or dart in general), if yes, then please share a sample code for such a single page application.
AMD is made largely irrelevant by the fact that Dart comes with a complete module system that is built into the language. That is the purpose of the import statement. Dart files are dynamically loaded in Dartium. If you are using the darttojs transpiler, it will create a single large file. The solution to this is to use use the DeferredLibrary class which will cause darttojs to create separate javascript files for each annotated import
See Seth Ladd's blog post for more details.

Where should I put my dart files if they are the server side files of restful api providers?

I'm trying to use start to write some restful apis for clients.
The content is looking like:
import 'package:start/start.dart';
import 'myfile1.dart';
import 'myfile2.dart';
import 'myfile3.dart';
import 'myfile4.dart';
void main() {
start(public: 'web', port: 3000).then((Server app) {
app.get('/').listen((request) {
request.response
.send('Hello, dart');
});
app.get('/aaa').listen(...);
app.get('/bbb').listen(...);
app.get('/ccc').listen(...);
app.get('/ddd').listen(...);
});
}
Notice there are also some other dart files myfileN.dart in the same dir.
Where should I put them?
According to the dart project layout, there are several entry points:
bin
lib
web
example
test
But I don't know where should I put my dart files. Since this is not a library, it's just an server-side application, and is not a web app, not a test, not an example, and not a command-line script.
I think there should be a src to put my files, but there is not. How to solve this problem? Doesn't the standard layout doesn't design well?
You can create an src within lib and put your files there.
If the files are executable, you can put them inside bin, or within a directory inside bin.
See the package layout conventions for details.
Many applications use third-party software.
Third-party software often organized as pluggable functionality.
Pluggable functionality often implemented not as independent executables but as linked libraries.
Also when you wrote application then you often divide it on different functionality.
The best way in this case (in Dart) put different functionality in different libraries.
This simplify your life when you will be testing and maintain your application.
By convention libraries in Dart resides in 'lib' directory.
Also implementation of libraries by convention must be located in 'lib/src' directory.
There may also be other possible explanations for this need.
I just tried explain it to you as simple as possible.

Resources