Dart vs Kotlin: detailed comparison
By: Souvik Biswas
Tags: Kotlin, Android, Flutter, Dart, Comparison

Originally published in Codemagic blog.

Dart vs Kotlin? Both Dart and Kotlin have been gaining a lot of traction in recent years, though they have been around for a pretty long time.

In the Google I/O 2019, they announced Android to become Kotlin-first.

Similarly, Dart also came into the mainstream as a cross-platform app development (Android, iOS, Web, Desktop, etc.) language used by Flutter.

Until now, you may have thought that Dart (used by Flutter) is the only reasonable option between these two languages if you want to build cross-platform apps and that if you are only getting into Android development, then Kotlin is the best choice.

However, KMM (Kotlin Multiplatform Mobile) also provides cross-platform app support. We will discuss it in more detail later in this article.

Disclaimer: The focus of this article would be to capture the essence of both languages rather than their partner frameworks.

About the languages — Dart and Kotlin

Dart was founded by Lars Bak and Kasper Lund, and version 1.0 of the language was released in November 2013. Initially, Dart was created with a focus on web applications, but since then, there have been a lot of changes up until the August 2018 release of Dart 2.0, which was optimized for writing cross-platform applications. Dart was also used at Google for some large-scale production applications, the most well-known being AdWords.

Kotlin was created by Andrey Breslav, later backed by JetBrains, and distributed under the Apache 2 Open Source license. Version 1.0 was released in February 2016. Kotlin is a modern, expressive and concise programming language that helps in reducing common coding errors, and its interoperability with Java gave it a boost among the people who were already in the Java ecosystem.

Type system

A type system is a set of rules that determine the types of a language construct. This set of rules allow us to ensure that all the parts of our program integrate consistently, also a clear and robust type system allows the compiler to execute low-level optimizations that improve the code.

Kotlin is a statically typed programming language. Statically typed languages are constrained to only support a known set of types. But, in order to facilitate interoperability with JavaScript code, Kotlin/JS offers the dynamic type. Using dynamic type basically turns off Kotlin's type checker.

val someString: dynamic = "hello"

However, you cannot use this dynamic type in regular Kotlin code targeting the JVM, as it will produce the following error:

error: unsupported [Dynamic types are not supported in this context]

Some of the main properties of the Kotlin type system are as follows:

Hybrid static, gradual, and flow type checking

Null safety

No unsafe implicit conversions

Unified top and bottom types

Nominal subtyping with bounded parametric polymorphism and mixed-site variance

More information about the Kotlin type system here.

Dart 2 is also a statically typed language. It supports sound type, now further extending it with sound null safety support. Soundness guarantee that types are correct via a combination of static and (when needed) runtime checking. Also generates smaller and faster code, particularly in an ahead-of-time (AOT) setting.

int getAge(Animal a) {

return a.age;

}

The above code when compiled in:

Dart 1 version (1.24.3), this method mapped to 26 native x64 instructions

version (1.24.3), this method mapped to 26 native x64 instructions Dart 2.12, this code maps to just 3 instructions

Check out this article for more information.

Even with type-safe Dart, you can annotate any variable with dynamic if you need the flexibility of a dynamic language. The dynamic type itself is static but can contain any type at runtime. Of course, that removes many of the benefits of a type-safe language for that variable.

More about the Dart type system here.

Some important points to note about Dart’s type system:

One benefit of static type checking is the ability to find bugs at compile time using Dart’s static analyzer.

var takes the type of the value which is first assigned, and doesn't allow changing the type.

var varName = "Souvik";

varName = 2;

The above code will produce the following compile-time error:

Error: A value of type 'int' can't be assigned to a variable of type 'String'.

If you want to store values of different types in a variable, you can use:

// using dynamic type

dynamic dynName = "Souvik";

dynName = 1;

You can customize Dart’s static analyzer and set up custom linting rules. Read more about this here.

Hello world

Before going deep into the syntax of the languages, let’s take a look at the very simple “ Hello world!” program, and see if there are any basic syntax differences between these two languages.

The Dart program consists of a single function named main containing a single line print statement “Hello world!”. Also, it ends with a semicolon.

Meanwhile, in the Kotlin program, a function is defined using the fun keyword followed by the function name main , which contains a very similar print statement to the one in the Dart program. But notice that here, a semicolon isn’t required to end a statement.

One more slight difference is that the Dart print function prints the output on a new line in the console, but the Kotlin print function prints on the same line if simultaneous calls are made. So, the println function in Kotlin is used to print on a new line.

Now, let’s look into other syntactical differences between these two languages.

The syntax

Dart and Kotlin are both modern programming languages. Here, we will see some simple syntactical comparisons between these two languages, sorted into a few major categories.

Variables & constants

Strings

Collections

Control flow

Functions

Extensions

Classes

Null safety

Kotlin’s type system uses null safety by default as it can distinguish between references that can hold null (nullable references) and those that can not (non-null references). This is aimed to eliminate NullPointerException from the code. The only possible causes of NPE may be:

An explicit call to throw NullPointerException()

Usage of the double-bang operator ( !! )

) Some data inconsistency with regard to initialization

Java interoperation

var newString: String = "android"

newString = null // compilation error

The Kotlin code above shows the default String type declaration, which doesn’t allow the variable to be null .

To allow null values, we can declare a variable like this:

var nullableString: String? = "android"

nullableString = null // ok

To get a better idea of Kotlin null safety and the operators related to it, check out the documentation.

Dart’s null safety is currently in beta. Unlike Kotlin’s null safety, Dart supports sound null safety. Soundness enables more compiler optimizations and helps to run even faster as compared to normal null safety. If the type system determines that something isn’t null, then that thing can never be null. Once the entire project is migrated to Dart’s null safety (including its dependencies), you can take full advantage of soundness, resulting not only in fewer bugs but smaller binaries and faster execution.

String newString = "flutter";

newString = null; // not allowed

In order to make it nullable, you have to declare like this:

String? nullableString = "flutter";

nullableString = null; // allowed

Read more about Dart’s sound null safety here.

Asynchronous operation

Unlike many other languages with similar capabilities, async and await are not keywords in Kotlin and are not even part of its standard library. Kotlin’s best approach to asynchronous programming is through the use of coroutines. However, there were a number of approaches to solving this problem in the past, like:

Threading

Callbacks

Futures, Promises

Reactive Extensions

In the past, threads were the most well-known approach to prevent applications from blocking. But there are a lot of limitations of threading:

Threads aren’t cheap

Threads aren’t infinite

Threads aren’t always available

Coroutines were presented to solve all these issues. Coroutines are basically lightweight threads.

A simple example of Kotlin code using a coroutine:

The output will be:

Hello,

World!

One more very important concept that goes alongside coroutine is Kotlin Flow, which helps in returning multiple asynchronously computed values.

To perform asynchronous operations in Dart, you can use the Future class with the async and await keywords.

A future represents the result of an asynchronous operation and can have two states: uncompleted or completed.

A simple example of Dart async/await using Future :

The output will be:

Awaiting user order...

1

2

3

4

Your order is: Italian pasta

Check out this codelab on Dart asynchronous programming for more information.

Usage in their partner frameworks

As I discussed earlier, both of these languages gained a lot of attention due to their partner frameworks, Kotlin for Android and Dart for Flutter. Flutter’s basic architecture is more UI-centric than that of Android, and Dart is really a good fit for this approach.

Let’s take a look at a very simple “Hello, world” application built using these two frameworks. Here, a single button is used to change the text from “Initial Text” to “Hello world!” and vice versa.

Android

The Kotlin code is as follows:

And the activity_main.xml layout code is:

Flutter

Similarly, the Dart code of the Flutter app is as follows:

Cross-platform support

Dart vs Kotlin — which has better cross-platform support?

Flutter (or Dart) is known for its amazing cross-platform performance on mobile, web and desktop from a single codebase. It provides close-to-native performance on both the Android and iOS mobile platforms. Also, Flutter Web’s performance has improved a lot since the introduction of the CanvasKit renderers (reaching close-to-native web app performance).

Kotlin also has a type of cross-platform support called Kotlin Multiplatform Mobile (KMM). It allows you to use a single codebase for the business logic of iOS and Android apps and implement a native UI by writing platform-specific code on top of it.

A getting started guide for KMM is available here.

Dart and Kotlin, both of the languages have a robust tooling ecosystem which is growing rapidly.

Tooling

Both the Dart and Kotlin languages have a robust tooling ecosystem that is growing rapidly.

CLI support

Both of the languages provide CLI support, so you can run Dart and Kotlin apps from the terminal.

Running Kotlin apps from CLI:

kotlinc demo.kt -d demo.jar

java -jar demo.jar

Learn more about the Kotlin CLI here.

Running Dart apps from CLI:

dart demo.dart

Learn more about the Dart CLI here.

IDE

The IDEs that you can use for writing Kotlin code are:

For building your Dart apps, the IDEs you can use are:

Dependencies/packages

You can use Gradle and Maven dependencies along with your Kotlin app.

You can include dependencies in your Dart project using the pub package manager by defining them inside the pubspec.yaml file. You can easily search for any Dart/Flutter package by going to pub.dev.

More about Dart package dependencies here.

CI/CD

There is an awesome range of CI/CD support for Android apps built using Kotlin. Some of the most well known of them are as follows:

Codemagic provides first-class support for Flutter and Dart apps.

Check out the Getting Started guide here.

Conclusion

This article should have provided you with enough information about both Dart and Kotlin that if you want to move from one language to the other, you have some idea about the syntactical differences between them, as well as the ecosystems of the languages.

References