BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Dart 2.1 Brings New Language Features, Better Usability and More

Dart 2.1 Brings New Language Features, Better Usability and More

Leia em Português

This item in japanese

Google has released Dart 2.1. This version brings new language features, performance improvements, faster type checks, and better usability for type errors.

Dart 2, released early this year, had brought a new compiler support for generating code for mobile devices, a reworked web platform tools, and a sound type system (also known as type safe system) to support teams building complex apps.

The sound type system helps users during the development, informing when a developer violates a contract specified by the types. Now on Dart 2.1, both edit-time and compile time type checks are completed.

Dart 2.1 has improved the support for mixins. Mixins are a feature that enable developers to reuse a class's code in multiple class hierarchies. A new syntax for mixins has been added, the keyword mixin can be used to define classes that can only be used as mixins. It is important to notice that the new syntax mixins prevent users to extending or constructing a mixin class.

Now mixins can extend other classes (previously they could only extend Object) and invoke methods in their superclass. An example of extending non-Object classes is from Flutter's animation APIs, where the SingleTickerProviderStateMixin declares a mixin that implements the general TickerProvider interface.

mixin SingleTickerProviderStateMixin on State implements TickerProvider {
  ...
}

The new mixin syntax is required for developers who want to use super inside a mixin.

class Superclass {
  superclassMethod() {
    print("in superclass");
  }
}

mixin SomeMixin on Superclass {
  mixinMethod() {
    // This is OK:
    super.superclassMethod();
  }
}

class GoodSub extends Superclass with SomeMixin {}

class BadSub extends Object with SomeMixin {}
// Error: Since the super() call in mixinMethod() can't find a
// superclassMethod() to call, this is prohibited.

Dart 2.1 now infers where an int can be evaluated as a double, an issue commonly experienced by Flutter developers when using an int type where the API expects a double type.

(Image taken from medium.com/dartlang)

Flutter and web developers can benefit from performance improvements. The cost of the type checks has been reduced in Dart 2.1, both for AOT-compiled code and for code run in the VM with JIT compilation. According to Google, performing a code analysis of a large benchmark app that used to take 41 seconds, now takes 25 seconds. In addition, dart2js minified output size has reduced by 17%, and the compilation time is 15% faster.

Dart 2.1 is now officially supported for protocol buffers. Protocol buffers are a language-neutral, platform-neutral mechanism for serializing structured data, the languages supported are Java, Python, Objective-C, C++, Dart, Go, Ruby, and C#. More information about protocol buffers can be found in the reference documentation.

Other features available include:

  • Added HashMap.fromEntries and LinkedHashmap.fromEntries constructors.
  • Added operators /, | and ^ to bool.
  • Added new HTTP status codes
  • Added -O flag to tune optimization levels. For more details run dart2js -h -v.

The complete list of features, bug fixes, and breaking changes can be found in the changelog.

Dart SDK 2.1 is available for download on the Dart homepage. For Flutter developers, Dart 2.1 is included as part of the Flutter 1.0 release. Google has also made available some samples for int-to-double and mixins.

Dart plugins exist for a series of IDEs and editors such as, Android Studio, Visual Studio Code, Intellij IDEA, Atom, Vim and Emacs.

Rate this Article

Adoption
Style

BT