Dart learning
Learning about the Dart language by making some example applications in it.
Set up the project
- Make sure you have Dart installed
- Run
pub getto download dependencies - Start the console app with
pub run bin/console_app.dart- Or (because the package name is
console_app, you can just runpub run console_app) - You can also run a dart file with
dart bin/console_app.dart
- Or (because the package name is
Key points
- pub is a a CLI tool for managing dependencies in Dart projects
pubspec.yamlis thepackage.jsonof Dart projects i.e. manifest file- In order to add a dependency, you must manually edit
pubspec.yamland add the package at the specified version range to the desired dependencies list - Dart comes with a linter. It's configured in
analysis_options.yaml - Dart is a strongly-typed language like Java or C#. There are many in-built types like
List<T>orMap<String, int>. See Dart's Language tour for details of every feature the language has (many). - Dart has no
privateorpublickeywords - it instead uses a convention-based a approach. A leading underscore for private members e.g._handleErrorwill not be available outside of the file they are declared in. - stagehand is a CLI tool for generating Dart projects
- Dart projects make a distinction between Application packages and Library packages
- An Application package is a standalone application that you run e.g. some website you made or a CLI tool
- A Library package is a package to be referenced by other packages i.e. it is intended to be a dependency - it won't have any
main()function.
- Dart projects have a bit of a peculiar structure to them
- "Application packages" usually still have a "Library package" where most of your code will live i.e. your package will import and use itself
bin/Your executables will live here (for Application packages mostly)lib/The source code for your "Library" (even if you are writing an Application package)- This will usually contain a top-level file that represents your Library's public API - it will export all the members from within your package
lib/src/The actual source code for your package
example/Examples of how to use your packagetest/Testsdoc/Package documentation often generated by dartdoctools/For scripts and things- For full details see Package layout conventions
Libraries
- console_app
- A sample application that cats files and implements some basic command line flags
- web_server
- A sample web server app