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.
Slides
Slide notes are available here.
The resources:
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)
Basics
… or look below:
.dart
file 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
var
. final
modifier makes variable assign-once;const
modifier 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, likepub
(import 'package:test/test.dart';
) - you can alias a lib, import only one symbol or exclude symbol from import (
as
,show
,hide
) - asynchrony support with
async
andawait
, 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
call
method 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) _predicate
orfinal List<void Function(Event)> _observers;
- Control structures:
for
(with iterator in formfor elem of iter
),while(){}
,do{} while()
,switch
(without pattern matching) - Extension methods add functionality to existing classes. Resolve to a static type of the receiver (like in Kotlin).
Classes
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.
See classes section in lnguage tour.
If, by any chance, you are a JavaScript developer, you might want to read a decicated guide for JavaScript prorgammers with examples.
Libraries
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.
Standard library
These libraries workk on all platforms:
- dart:core
- dart:async and package:async
- dart:collection and package:collection
- dart:convert and package:convert
- dart:developer (only in Native JIT and dartdevc)
- dart:math
- dart:typed_data (and also package:typed_data)
Dart native
Dart native platform allows to create AOT- and JIT-compiled code and provides a few special libraries:
Javascript
Dart Web platform allows developing for web.
- dart:html
- dart:indexed_db
- dart:js
- dart:js_util
- package:js - also JavaScript interoperability
- dart:svg
- dart:web_audio
- dart:web_gl
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…
Next steps
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!
Happy coding!
Ten wpis jest częścią serii Dart.