Dart Excitement From Kotliner Perspective

Marcin Oziemski
The Startup
Published in
3 min readJun 5, 2019

--

When I first looked at Dart with Flutter some years ago I must say I was very skeptic about it. There were so many things that I loved in new languages like Kotlin that Dart didn’t have. The nesting Widgets to the point that code looked like pyramids from Egipt was just… but right now I think it is on a better way than ever to become one of my favorite. Let me explain why.

original photo by Nathan Dumlao

Cascade

This two dot operator “..” can save you creating a builder or just write more readable code.
It allows you to make a sequence of operations on the same object. Something similar we can achieve with apply {…} extension function in Kotlin.

Kotlin:

Dart:

Spread

The spread operator which I first came across in JavaScript spread into Kotlin and now (from v. 2.3) to Dart.

The notation differs in Kotlin as is represented by an asterisk (*) but the idea behind is the same. It unwraps a collection of items.

In Kotlin from what I know, it’s only possible for Arrays:

In Dart beside normal spread operator we also have a null-aware version of it …?, besides it can operate on more kinds of collections like sets or maps.

Still, it’s a statically typed language and can’t do magic like JS with unwrapping objects.

Nullability

Kotlin’s type system is aimed at eliminating the danger of null references from code.

We don’t have that in Dart, YET! As on Google I/O 2019 they announced that they are working on non-nullable types!!! How great is that?! 👏😲

#Edit: Sound null safety is available now in Dart 2.12 and Flutter 2.

Besides the types itself, they have similar basic null checking operators like in Kotlin:

  1. Elvis operator in Kotlin ?: is that double question mark ?? in Dart.
  2. The conditional member access ?. is the same in both of them.

The Kotlin has some more like the not-null assertion operator !! or safe cast as? but Dart has “if null assignment” ??= that assign value only if the assigned-to variable is null.

Mixin

Mixin is not a new thing in Dart but its a new concept for most of known to me languages. I can think of only one kind of similar-ish thing that could be achieved in Kotlin by Delegates, where you implement an interface by constructor param.

If you want to know more about Delegates in Kotlin this is a great article about them.

Ok, but let go back to what are mixins?
So we all know that class can extend only one class, but what if we would like to extend additional type with state and functions to our class? Here where mixin comes in!

Like in Kotlin example above we can extend parent class but instead of adding that additional state and functions by params that implement the interface we just add mixin.

Then Wings mixin could look like this:

If you need more explanation on what are mixins check this article.

Optimized for UI

This is a slogan from the new dart.dev site, and with new Dart 2.3 this is more of the truth than before.
On the beginning of this article, I was writing that my first impression of building widgets was code that looked like pyramids, but with this new release, we can eliminate nesting widgets in many ways.

We can use if’s, for’s and spread operator inside the Widgets declaration:

For more detailed information go to this article.

Summary

Besides the couple things I’ve just described there are async-await, RxDart and many other things that make me more and more excited about Dart and the way it’s progressing.
I think that with the growing popularity of Flutter, Google and open source community (as Dart is open-source) can really make it competing in terms of popularity.

Originally published at https://www.netguru.com.

--

--

Marcin Oziemski
The Startup

Senior Android and Flutter Developer. Working at Netguru.