dart
A google language rather like C. It is however a scripting language and mainly made for the web. It is easily trans coded to javascript.
functions
Optional parameters can be specified between [] , and optional named parameters can be specified between {}.
In the flutter dart example, an unexplained notation appeared like this :
| functions | |
|---|---|
This was actually two dart constructs in one. The first is that you don't need to specify a body to the Choice() constructor if all it does is initialize the member variables. It concludes from the parameters that that is what you want.
The second is the optional parameters:
Optional positional
| positional parameters | |
|---|---|
Optional named :
| named parameters | |
|---|---|
So that explains the Choice() class constructor, it has optional parameters for initializing it's members.
There is even more constructor types, one of them is named constructors. This lets you add another name for more specific constructors :
pub
Pub is the package manager for dart. "Flutter packages" uses it under the hood. There are two important files involved :
- pubspec.yaml : You can create and edit this
- pubspec.lock : This one is generated to 'freeze' packages at certain versions.
Pubspec is also important for creating packages :
At the very minimum, a Dart package is simply a directory containing a pubspec file.
And ...
If your app uses one or more packages, then your app itself must be a package.
If you want to use a library in a dart package, you need to :
- create a pubspec.yaml file and specify the dependency.
- run pub get
- import the package in the source
pubspec.yaml
You need at least a name for your package, and probably some dependencies as well. The pubspec.yaml file defines the top directory of your package.
| pubspec.yaml | |
|---|---|
See visit for a complete description of the pubspec.yaml format.
Versions are 3-number normally, but you can also have an optional suffix like : +hotfix.problem or -alpha.12
Packages are published at a specific version. After that these are frozen, you need a new version to change anything again.
You are encouraged to use semantic versioning, where the 3-number (X.Y.Z) are of the form major.minor.patch.
details : visit
But in very short :
Semantic versioning means declaring a public API, increment versions by +1 only and reset to 0 of following numbers. The rest is a number of rules you have to follow with discipline.
All this to avoid so-called dependency-hell.
Dependency hell is exactly what you think it means.
The dependencies are the main reason for public.yaml's existence. They are split in dependencies and dev_dependencies, for obvious reasons like unittest libraries etc. There is also a third flavour called : dependency_overrides, which does what you expect it does.
You only specify direct dependencies, pub will handle the transitive dependencies for you.
You can show the dependencies of a package with:
| dependencies | |
|---|---|
creating local packages
I see this as a library in C. This also goes for flutter !, so here is a small migration from the dispatchpage from lets_play into a separate library.
First create the library package
Flutter has a command for that, it creates the directory structure needed.
| flutter | |
|---|---|
Now at this point they leave you with a very small file with a very small example function :
| library | |
|---|---|
import the new local library
There is nothing to compile here, you could just test this function by including it from the anniversary app. You need an entry in pubspec.yaml for that and of course an import.
| import library | |
|---|---|
I left the dispatchpage import because we have not moved it to the bubl lib yet. Now do 'flutter packages get' to setup the library. You could now use the Calculator class, I have not tried it by the way.
move the functionality to bubls package
Now move the dispatchpage.dart file to bubls/lib/src.
the lib/src directory is meant for the internal sources of the package, just one .dart file called the same as the package should remain in the lib/ directory.
So the layout would be now :
| directory structure | |
|---|---|
But since we want to use dispatchpage.dart functionality we don't want it to remain private see we have to export it, changes bubls.dart to :
| export | |
|---|---|
Now in theory ;) you should be able to run anyversary again.