$30 off During Our Annual Pro Sale. View Details »

Dart for Kotliners

Dart for Kotliners

When you are trying to learn something new it is always very helpful to relate the new concepts and information to your prior knowledge.

That is exactly what this article is meant for. Applying all your existing Kotlin knowledge and expertise to learn the Dart programming language as a first step to get into Flutter development.

https://davidmigloz.medium.com/dart-for-kotliners-eb6d6a6676b

Avatar for David Miguel

David Miguel

April 22, 2021
Tweet

Other Decks in Programming

Transcript

  1. Agenda • What is Dart? • Type system • Variables

    & Constants • Operators • Conditional expressions • Loops • Functions • Classes • Collections • Asynchronous programming • Dependency management • IDEs • Learning resources
  2. What is Dart? Language Dart Kotlin Developed by Google -

    2011 JetBrains - 2011 1st stable release 2013 2016 Paradigm Imperative, Object-oriented, Functional Imperative, Object-oriented, Functional Type system Statically typed, null-safe (with dynamic type support) Statically typed, null-safe (with dynamic type support in Kotlin/JS) Target platforms ◦ Dart/DVM (development & backend) ◦ Dart/Web (Web) ◦ Dart/Native (iOS, Android, macOS, Linux, Windows, Fuschia, backend, iOT) ◦ Kotlin/JVM (Android, backend) ◦ Kotlin/JS (Web) ◦ Kotlin/Native (iOS, macOS, Linux, Windows)
  3. Productive: Iterative development • Android, Desktop & Backend: Dart Virtual

    Machine (DVM) + Just In Time (JIT) compiler. • iOS: Dart Virtual Machine (DVM) + Dart Byte Code (DBC) interpreter. • Web: Dart development compiler (dartdevc).
  4. Portable: with native performance • Native: Dart code → Ahead

    Of Time (AOT) compiler → native x86/ARM binary code • Web: Dart code → dart2js compiler → optimized JavaScript • Backend: Dart code → DartVM + (Just In Time) JIT compiler Dart code → Ahead Of Time (AOT) compiler → native x86/ARM binary code
  5. Robust • Sound, null-safe type system It uses a combination

    of static and runtime checks to guarantee that an expression of one type cannot produce a value of another type. • Google-class scalability & dependability Google AdMob / Google Pay / Google One / Stadia / Google Ads / Nest Hub Cloud search / Google Shopping / Google Analytics...
  6. Numbers Language Dart Kotlin Integer int 64 bit (53 bit

    in JS) Byte 8 bit Short 16 bit Int 32 bit Long 64 bit Floating point double 64 bit (double- precision) Float 32 bit (single- precision) Double 64 bit (double- precision)
  7. String Language Dart Kotlin Strings String UTF-16 code units sequence

    String UTF-16 code units sequence Char 16-bit Unicode character
  8. Variables & Constants Language Dart Kotlin Mutable variables var var

    Immutable variables final val Constants const const val
  9. Arithmetic operators Language Dart Kotlin Addition + + Subtraction -

    - Negation -expr -expr Multiplication * * Division / / (Integer division if both operands are Int) Integer division ~/ Modulo % % Increment ++ ++ Decrement -- --
  10. Relational operators Language Dart Kotlin Greater than > > Lesser

    than < < Greater equal >= >= Lesser equal <= <= Identity identical() === Equality == By default, object equality is implemented via identical() == By default, object equality is implemented via === Not equal != != Collection equality IterableEquality().equals() From package:collection/collection.dart ==
  11. Logical operators Language Dart Kotlin AND a && b a

    && b OR a || b a || b NOT !a !a
  12. Bitwise operators Language Dart Kotlin Bitwise AND a & b

    a and b Bitwise OR a | b a or b Bitwise XOR a ^ b a xor b Bitwise NOT ~ a a.inv() Left shift a << b a shl b Right shift a >> b a shr b
  13. Type check and cast operators Language Dart Kotlin Is type

    is is Is not type is! !is Unsafe cast as as Nullable cast as?
  14. Nullability operators Language Dart Kotlin Not-null assertion a! (bang operator)

    a!! Safe access ?. ?. If null ?? ?: (elvis operator) Assign if var is null ??=
  15. Conditional expressions Language Dart Kotlin If else if(condition) { }

    else if { } else {...} if(condition) { } else if { } else {...} Ternary conditional condition ? a : b; if(condition) a else b If else cascade switch(option) { case option1: ... break; default: ... } when(option) { option1 -> … else -> ... }
  16. Loops Language Dart Kotlin For loop for (initial; condition; step)

    {...} for (index in range step x){...} For in for (variable in iterable){...} for (variable in iterable){...} For each iterable.forEach((it) {...}) iterable.forEach {...} While while (condition) {...} while (condition) {...} Do while do {...} while (condition); do {...} while (condition) Labels labelName: for (var i = 1; i < 5; i++) { … break labelName; } labelName@ for(x in 1..5) { … break@myLabel }
  17. Functions Language Dart Kotlin Main void main() {...} void main(List<String>

    args) {...} fun main() {...} fun main(args: Array<String>) {...} One line void function() => …; fun function() = …; Optional positional param void function([String? arg]){...} function(); / function(“string”); fun function( arg: String? = null ) {...} function() function(“str”) function(arg = “str”) Optional named param void function([String? arg]){...} function(); / function(arg: “str”);
  18. Functions Language Dart Kotlin Lambda (String arg) => ...; {arg:

    String -> ...} High order functions void function( int myFunction(String arg) ) { print(myFunction(“string”)); } fun function( myFunction: (arg: String) -> Int ) { print(myFunction(“string”)) } Reference to function void function() {...} final ref = function fun function() {...} val ref = ::function
  19. Collections Language Dart Kotlin Lists List<T> List<T> / MutableList<T> Array<T>

    Set Set<T> Set<T> / MutableSet<T> Map Map<T> Map<T> / MutableMap<T> Queue Queue<T>
  20. Collection operations: filtering Language Dart Kotlin first / last first

    / last firstWhere / lastWhere where filter take take every all any any
  21. Asynchronous programming How do we write some code that waits

    for something most of the time? • Kotlin: coroutines + suspending functions Behind the scenes: Continuation-Passing Style (CPS) • Dart: futures + await / async Behind the scenes: Event loop
  22. Asynchronous programming Language Dart Kotlin Async function Future<String> asyncFun() async

    {} suspend fun asyncFun(): String {} Sequential async call await asyncFun(); (inside an async function) asyncFun() (inside a coroutine) Concurrent async call var future1 = asyncFun(); var future2 = asyncFun(); await future1; await future2; (inside an async function) var deferred1 = async { asyncFun() } var deferred2 = async { asyncFun() } deferred1.await() deferred2.await() (inside a coroutine)
  23. Asynchronous programming • Kotlin: calls to suspending functions are sequential

    by default. Only if you explicitly call async{} the execution will be concurrent. • Dart: calls to asynchronous functions are concurrent by default. Only if you await the asynchronous function then the execution will be sequential.
  24. Streams Single value Multiple values Single value Multiple values Sync

    int Iterator<int> Int Iterator<Int> Async Future<int> Stream<Int> Deferred<Int> Flow<Int>
  25. IDEs plugins for Dart • Officially supported: ◦ Android Studio

    ◦ IntelliJ IDEA (and other JetBrains IDEs) ◦ Visual Studio Code • Community supported: ◦ Emacs ◦ Vim ◦ Eclipse
  26. Learning resources • Language tour: https://dart.dev/guides/language/language-tour • Tutorials: https://dart.dev/tutorials •

    Codelabs: https://dart.dev/codelabs • Books: https://dart.dev/resources/books • Dart language specification: https://dart.dev/guides/language/spec • Dart language repository: https://github.com/dart-lang/language