Written by
Natig Babayev
on
on
From Kotlin to Dart 2 Cheat Sheet - Basics
Kotlin
println("Hello, World!")
Dart
print('Hello, World!');
Variables I
Kotlin
val x: Int = 0
val y = 1
Dart
final int x = 0;
final y = 1;
Variables II
Kotlin
var w: Int
var z = 2
z = 3
w = 1
Dart
int w;
var z = 2;
z = 3;
w = 1;
Null I
Kotlin
var name: String? = null
println(name) // prints 'null'
name = "Natig"
println(name) // prints 'Natig'
val surname: String? = null
println(surname) // prints 'null'
Dart
String name; // by default, it's null
print(name); // prints 'null'
name = 'Natig';
print(name); // prints 'Natig'
final String surname = null; // final variable must be initialized
print(surname); // prints 'null'
Null II
Kotlin
var name: String? = null
val length = name?.length
println(length ?: 0) // prints '0'
name = name ?: "Undefined"
println(name) // prints 'Undefined'
Dart
String name; // by default, it is null
final length = name?.length;
print(length ?? 0); // prints '0'
name ??= 'Undefined';
print(name); // prints 'Undefined'
Strings I
Kotlin
val name = "Natig"
val surname = "Babayev"
val sentence = "I am $name $surname."
println(sentence) // prints 'I am Natig Babayev.'
val anotherSentence = "I am ${name.substring(0, 1)}. $surname."
println(anotherSentence) // prints 'I am N. Babayev.'
Dart
final name = 'Natig';
final surname = 'Babayev';
final sentence = 'I am $name $surname.';
print(sentence); // prints 'I am Natig Babayev.'
final anotherSentence = 'I am ${name.substring(0, 1)}. $surname.';
print(anotherSentence); // prints 'I am N. Babayev.'
Strings II
Kotlin
val text = """
First Line
Second Line
Third Line
"""
Dart
final multiLineString = '''
First Line
Second Line
Third Line
''';
Conditional expressions
Kotlin
val result = if (grade > 5) "grade > 5" else "grade <= 5"
Dart
final result = grade > 5 ? 'grade > 5' : 'grade <= 5';
As, Is
Kotlin
val a = 12
if (a is Int) {
val result = a / 2
println(result) // prints '6'
}
val length = (a as String).length
println(length) // throws exception
Dart
final a = 12;
if (a is int) {
final result = a / 2;
print(result); // prints '6'
}
final length = (a as String).length;
print(length); // throws exception
When / Switch
Kotlin
val a = .. // some value
val result = when (a) {
in 0..2 -> "0..2"
3, 4 -> "3 or 4"
5 -> "it is 5"
else -> "Unknown"
}
println(result)
Dart
final a = ..; // some value
var result;
switch (a) {
case 0:
case 1:
case 2:
result = '0..2';
break;
case 3:
case 4:
result = '3 or 4';
break;
case 5:
result = 'it is 5';
break;
default:
result = 'Unknown';
}
print(result);
For
Kotlin
for (i in 1 until 5) { }
for (i in 1..6 step 2) { }
for (item in collection) { }
Dart
for (var i = 1; i < 5; i++) { }
for (var i = 0; i <= 6; i += 2) { }
for (var item in collection) { }
Collections I
Kotlin
val numbers = listOf(17, 3, 1, 2)
val map = mapOf(1 to "One",
2 to "Two",
3 to "Three")
val mutableList = mutableListOf(1, 2, 3)
Dart
final numbers = List.unmodifiable([17, 3, 1, 2]);
final map = {
1: 'One',
2: 'Two',
3: 'Three'
};
final mutableList = [1, 2, 3]
Collections II
Kotlin
val rangeOfNumbers = (0..12).toList()
val mutableRangeOfNumbers = (0..12).toMutableList()
Dart
final rangeOfNumbers = List.generate(13, (index) => index, growable: false);
final mutableRangeOfNumbers = List.generate(13, (index) => index);
Collections III
Kotlin
numbers.forEach(::println)
numbers.filter { it > 5 }
.forEach { value -> println(value) }
Dart
numbers.forEach(print);
numbers.where((it) => it > 5).forEach((value) => print(value));
Share on: