Dart Excitement From Kotliner Perspective

Photo of Marcin Oziemski

Marcin Oziemski

Updated Feb 14, 2023 • 6 min read
nathan-dumlao-1112682-unsplash

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.

nathan-dumlao-1112682-unsplash

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:

val person = Person().apply { 
name = "Marcin"
surname = "Oziemski"
}

Dart:

final person = Person() 
..name = "Marcin"
..surname = "Oziemski"
..age = 99

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:

val s = arrayOf("d", "e", "f")
val s2 = listOf("a", "b", "c", *s)
println(s2) // [a, b, c, d, e, f]

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.

final s = ["d", "e", "f"];
final s2 = ["a", "b", "c", ...s];
print(s2); // [a, b, c, d, e, f]
var s;
final s2 = ["a", "b", "c", ...?s];
print(s2); // [a, b, c]

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?! 👏😲

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.

// Assign a value to b if b is null; otherwise, b stays the same
b ??= value;

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.

class Bird(wings: Flying) : Animal, Flying by wings

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.

class Bird extends Animal with Wings

Then Wings mixin could look like this:

mixin Wings { 
String speed = "fast"

void fly() {
print('Flying $speed')
}
}

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:

Widget build(BuildContext context) { 
return Column(children: [
Text(mainText),
...buildMainElements(),
if (page != pages.last)
FlatButton(child: Text('Next')),
for (var section in sections)
HeadingAction(section.heading),
]);
}

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.

Photo of Marcin Oziemski

More posts by this author

Marcin Oziemski

Engineering Lead at Netguru
Lost with AI?  Get the most important news weekly, straight to your inbox, curated by our CEO  Subscribe to AI'm Informed

Read more on our Blog

Check out the knowledge base collected and distilled by experienced professionals.

We're Netguru

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency.

Let's talk business