A Kotlin Developer Learning Dart

An Android developer perspective on the Dart language.

I decided to study Flutter and I have started taking lessons from the App brewery on the course called, “The Introduction to Flutter Development with Dart.” After I have finished the installation part, I think I could proceed faster if I understand the Dart syntax before taking any further lessons.  In this post, I will focus on language and syntax. The dart development team claims that this language is optimized for UI but I won't go deep into the compiler and its underlying layers since I still haven't studied Dart well enough to talk about that yet.

The syntax

I used to develop Android applications with Java several years ago, and currently, I use Kotlin as my main language. I would say I feel quite familiar with the syntax. To me, Dart looks more like Java than Kotlin. From my perspective, its syntax's style is somewhere between Java and Kotlin. You still have to put the semicolon in the code, and for me, that doesn't hurt much.

What Dart doesn't have compared to Kotlin.

I think now is a good time to start learning Dart compared to several years back. The current version of Dart is 2.8 and it has improved a lot from version 1.x. such as having the extension methods. Although the null safety is not included in the versions 2.8, the feature is coming close on its way. Here are a few things in Kotlin that aren't in Dart 2.8.

  • No null-safe types: In Kotlin we can use the `?` to specify whether each variable can be null or not. Currently, you need some overhead to check for nullability when working with a variable. However, as I have mentioned, the null-safe type is so close for releasing. It should be included within the next few versions.
  • Missing of data classes: In Dart, when working with the model classes, you would need to overwrite equals, toString, and hashcode, unlike Kotlin where data class automatically does this for you.
  • Sealed classes: This is a great feature of the Kotlin language. You can only define types in class. It’s like having an enum class with a superpower. If you have used it, you will notice that the sealed class is really handy. However, coming to Dart you lose this feature.

Comparing it point by point to Kotlin's would be unfair since Dart is not trying to be Kotlin 2 -  I just want to express my thoughts from the perspective of a Kotlin developer.


The language samples

In this section, I will look at the simple grammar of Dart language from the perspective of Kotlin developer. I won't mention Control flow statements such as if, for, and while since it looks just like usual control statements. 

Variables
Same as Kotlin, Dart is type-safe. Dart has the type inference feature so the variable doesn't need explicit type declaration. To declare a type inference variable use var.

var platform = 'Android';

The variable in Dart can be the dynamic type. To declare dynamic variables use the dynamic keyword.

dynamic platform = 'Android';

You can also specify the type explicitly.


String platform = 'Android';

There is no val and var concept like in Kotlin. To create the unchanged variable use const or final keywords.

final platform = 'Android';
const version = '1.0';

The const keyword can also be used for declaring the constant value.

var array = const []

Visibility Modifier
Unlike Java and Kotlin, Dart doesn't have the keywords public, protected, and private. To declare a private variable use an underscore (_) as a variable prefix.

Functions
Functions in Dart look much like Java method but you can omit the return type. 

bool isAndroidVersion(String atomicNumber) {  return _androidVersions[atomicNumber] != null;}
isAndroidVersion(String atomicNumber) {  return _androidVersions[atomicNumber] != null;}

Functions in Dart, including optional parameters, default parameter values. You can check out the syntax here.

Lambda
Dart also supports passing anonymous functions as an argument. Comparing to Kotlin the shorthand syntax change from -> to =>.

var androidVerions = ['Cupcake', 'Donut', 'Eclair', 'Froyo', 'Gingerbread', 'Honeycomb', 'Ice Cream Sandwich', 'Jelly Bean', 'Kitkat', 'Lollipop', 'Marshmallow'];
androidVersions.where((name) => name.contains('lol')).forEach(print);

Classes
Every class in Dart is an instance of Object.  There is not much difference between other object-oriented language classes and Dart's classes. The only thing that is new to me in Dart is named constructor. Normally, in Kotlin, we only use the primary constructor and rarely use the secondary constructor. However, in Dart, we can implement multiple constructors and give names to each of them.

class Point {
  double x, y;

  Point(this.x, this.y);

  // Named constructor
  Point.origin() {
    x = 0;
    y = 0;
  }
}

Extension methods
Same as the extension function in Kotlin, there is an extension method in Dart. You could say that once you start using the extension function in Kotlin, there is no going back. It would be sad if Dart didn’t have this feature. The declaration of the extension method is a bit different from Kotlin. Here is the example taken from the official site.

extension NumberParsing on String {
  int parseInt() {
    return int.parse(this);
  }

  double parseDouble() {
    return double.parse(this);
  }
}

Conclusion

Here is my summary on the comparison between Dart and Kotlin. It might not cover all of the points, but I just trying to point out the first thing that comes to mind when I started looking into Dart syntax. I haven't covered the asynchronous programming in Dart since it's worth talking about that in a future post.  I think I can work with Dart productively after scanning through the language.


Looking for a new challenge? Join Our Team

Ready to start your project? Contact Us



Sources

- https://dart.dev/guides/language/language-tour#classes
- https://dart.dev/samples
Like 1 like
Ben Kitpitak
Mobile Developer at OOZOU in Bangkok, Thailand
Share:

Join the conversation

This will be shown public
All comments are moderated

Get our stories delivered

From us to your inbox weekly.