Dart for Java developers
Welcome in a new series of articles about Dart programming language.
It is not a beginner tutorial. I focus mainly on features that may be surprising and/or interesting for a Java(Script) developer.
Slide notes are available here.
Start with these:…
- https://dart.dev/guides/language/language-tour (language overview)
- https://dartpad.dev/ (write & run in online editor)
- https://dart.dev/guides/language/spec (language specification page, current spec pdf: 2.10)
- https://dart.dev/guides/language/effective-dart (good practices)
… or look below:
.dartfile is a “module”.
- top-level functions, variables, constants and classes
- names starting with “_” (underscore) are private to the module
- everything is an object
- null-safety (nullable type by appending
?to type name, like in Kotlin)
- generics are are reified (accessible at runtime), contrary to Java, where types are erased.
- type inference; you can declare variables with
finalmodifier makes variable assign-once;
constmodifier makes variable a compile-time constant.
- import from built-in libraries (
import 'dart:html';), local filesystem (
import './foo.dart) or external package managed by build tool, like
- you can alias a lib, import only one symbol or exclude symbol from import (
- asynchrony support with
await, Future and Stream classes, which are run in same Isolate - see [Concurrency in Dart)[https://api.dart.dev/stable/dart-isolate)
- sync and async generator functions
- callable classes: instances can be called with params; implement
callmethod in the class
- isolates as “threads” without race conditions, see concurrency
- typedefs (
typedef IntList = List<int>;_), also generic, and (recommended) function types, eg:
final bool Function(Event) _predicateor
final List<void Function(Event)> _observers;
- Control structures:
for(with iterator in form
for elem of iter),
switch(without pattern matching)
- Extension methods add functionality to existing classes. Resolve to a static type of the receiver (like in Kotlin).
As Java developers we usually think in terms of classes. Dart supports classs, allows to define abstract classes and offers mixins (that define behaviors and don’t have constructors) with which a class can be extended.
If you want to dive deep into libraries, start with standard library. Then, depending on the target platform, look at native or javacript secions below.
These libraries workk on all platforms:
- dart:async and package:async
- dart:collection and package:collection
- dart:convert and package:convert
- dart:developer (only in Native JIT and dartdevc)
- dart:typed_data (and also package:typed_data)
Dart native platform allows to create AOT- and JIT-compiled code and provides a few special libraries:
Dart Web platform allows developing for web.
Nice cookbook articles:
The cookbook is a nice collection of hints on how to implement specific Dart/Flutter usecases, for example: parsing json in backgroud, return data from screen, make authenticated requests or persist data with sqlite, how to write integration tests…
As a next step, we may want to write some short prorgams just to check out some ideas and get used to the language itself. Stay tuned!
Ten wpis jest częścią serii Dart.