July 08, 2018

When Flutter started to become popular and more people picked up interest in it, the first thing that they noticed was that it used Dart as a programming language, and this disappointed a lot of people.

As Google already adopted Kotlin as a first-class language for Android, many wondered why they didn’t take the same choice with Flutter and why did they decide to go with a totally different language.

The reason for this is already explained really good here, but long story short it was impossible for Google to choose Kotlin for Flutter because it lacked some fundamentally features in order to make it work the way Google envisioned Flutter to work.

While I love both languages, I think they are not so different in terms of features, and while both have strenghts and weaknesses, they have also a lot of similarities, so let’s check them.

Hello world

Kotlin

println ( "Hello, world!" )

Dart

print ( "Hello, world!" ) ;

Variables And Constants

Kotlin

var myVar = 1 myVar = 2 val myConstant = 5

Dart

var myVar = 1 ; myVar = 2 ; const myConstant = 5 ;

Explicit Types

Kotlin

val explicitVar : Int = 7

Dart

int explicitVar = 7 ;

String Interpolation

Kotlin

val apples = 3 val oranges = 3 val message = "I have ${ apples + oranges } fruits"

Dart

var apples = 3 ; var oranges = 3 ; var message = "I have ${apples + oranges} fruits" ;

Arrays

Kotlin

val fruits = arrayOf ( "apple" , "orange" , "banana" )

Dart

var fruits = [ "apple" , "orange" , "banana" ] ;

Maps

Kotlin

val basket = mutableMapOf ( "apple" to 3 , "orange" to 2 ) basket [ "apple" ] = 5

Dart

var basket = { "apple" : 3 , "orange" : 2 , } ; basket [ "apple" ] = 5 ;

Functions

Kotlin

fun greet ( name : String ) : String { return "Hello $name " } greet ( "Bob" )

Dart

String greet ( String name ) { return "Hello $name" ; } greet ( "Bob" ) ;

Optional Arguments

Kotlin

fun addIntro ( title : String , subtitle : String = "No subtitle" , bold : Boolean = true ) { } addIntro ( "Title" ) addIntro ( "Title" , "My subtitle" ) addIntro ( "Title" , "My subtitle" , false )

Dart

void addIntro ( String title , [ String subtitle , bool bold ] ) { } addIntro ( "Title" ) ; addIntro ( "Title" , "My subtitle" ) ; addIntro ( "Title" , "My subtitle" , false ) ;

Classes

Kotlin

class Foo { private var bar = 0 }

Dart

class Foo { int _bar = 0 ; int get bar = > _bar ; set bar ( int val ) = > _bar = val ; }

Checking Type

Kotlin

val obj = "string" if ( obj is String ) { print ( obj . length ) }

Dart

var obj = "string" ; if ( obj is String ) { print ( obj . length ) ; }

Null safety

Kotlin

var a : String = "abc" a = null var b : String ? = "abc" b = null val length = b ? . length

Dart

var a = "abc" ; a = null ; var length = a . length ; length = a ? . length ;

Data classes

Kotlin

data class Box ( val width : Int , val height : Int )

Dart

Dart does not have this feature at the language level, but we can use code generation to get this. We can do this with the built_value package. It will look something like this:

abstract class Box implements Built < Box , BoxBuilder > { int get width ; int get height ; }

We will get the equals, hashCode, toString methods, and also other cool features like immutability.

Asynchronous code

Kotlin

fun heavyComputation ( ) { async { .. . val result = computation . await ( ) .. . } }

Dart

void heavyComputation ( ) async { . . . var result = await computation ( ) ; . . . }

I believe it’s really fun to learn new languages by comparing them with something we already know, so if you already know Kotlin I hope this will make it easier for you to learn Dart.