Dart cheat-sheet for Kotlin (Android) developers
Posted on September 16, 2019 in Flutter
This post is my effort to provide my fellow Kotlin developers a hand to pick up on Dart. Idea is to provide equivalent solution in both languages. The demonstrated code is not the only solution for a particular problem, but just one way of doing things. Style of article is to perform a task in both languages. For example, print "Hello World!" in Kotlin and Dart.
I've used Kotlin Playground and DartPad to run and play around with languages.
Task: Print "Hello World"
Kotlin:
fun main() {
println("Hello World")
}
Output:
Hello World
Dart:
void main() {
print("Hello World");
}
Output:
Hello World
Task: How to delete duplicates in a List
Kotlin:
fun main() {
var myList = listOf('A', 'A', 'B', 'C', 'A', 'D', 'B', 'C')
myList = myList.distinct()
print(myList)
}
Output:
[A, B, C, D]
Another way in Kotlin. Note usage of arrayOf()
instead listOf
.
fun main() {
val myList = arrayOf('A', 'A', 'B', 'C', 'A', 'D', 'B', 'C')
val noDupsList = myList.distinct()
print(noDupsList)
}
Output:
[A, B, C, D]
Dart:
//Delete duplicates from myList
void main() {
var myList = ['A', 'A', 'B', 'C', 'A', 'D', 'B', 'C'];
myList = Set.of(myList).toList();
print(myList);
}
Output:
[A, B, C, D]
Task:
Kotlin:
Output:
Dart:
Output:
Task:
Kotlin:
Output:
Dart:
Output:
Happy coding :)
Useful Links
Liked the article ? Couldn't find a topic of your interest ? Please leave comments or email me about topics you would like me to write ! BTW I love cupcakes and coffee both :)
Follow me at twitter