Contents

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:…

  1. https://dart.dev/guides/language/language-tour (language overview)
  2. https://dartpad.dev/ (write & run in online editor)
  3. https://dart.dev/guides/language/spec (language specification page, current spec pdf: 2.10)
  4. https://dart.dev/guides/language/effective-dart (good practices)

Basics

… or look below:

  1. .dart file is a “module”.
  2. top-level functions, variables, constants and classes
  3. names starting with “_” (underscore) are private to the module
  4. everything is an object
  5. null-safety (nullable type by appending ? to type name, like in Kotlin)
  6. generics are are reified (accessible at runtime), contrary to Java, where types are erased.
  7. type inference; you can declare variables with var.
  8. final modifier makes variable assign-once; const modifier makes variable a compile-time constant.
  9. import from built-in libraries (import 'dart:html';), local filesystem (import './foo.dart) or external package managed by build tool, like pub (import 'package:test/test.dart';)
  10. you can alias a lib, import only one symbol or exclude symbol from import (as, show, hide)
  11. asynchrony support with async and await, Future and Stream classes, which are run in same Isolate - see [Concurrency in Dart)[https://api.dart.dev/stable/dart-isolate)
  12. sync and async generator functions
  13. callable classes: instances can be called with params; implement call method in the class
  14. isolates as “threads” without race conditions, see concurrency
  15. typedefs (typedef IntList = List<int>;_), also generic, and (recommended) function types, eg: final bool Function(Event) _predicate or final List<void Function(Event)> _observers;
  16. Control structures: for (with iterator in form for elem of iter), while(){}, do{} while(), switch (without pattern matching)
  17. 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 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.

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.