Quick learning Dart (Flutter) with the Kotlin language (Part 2)

Tram Ho

1. Introduction

This series is written to help those who know the Kotlin or Java language quickly learn the Dart language to code Flutter. If you haven’t read part 1, you can read again part 1 here

2. Operators

Most of them are like Kotlin. However, there are a few other points, I have redlined and commented in the code.

In addition to the above operators:

  • The function call operator is the same as Kotlin () . Example: print(3)
  • The operator that calls the object’s member function or property is also . . For example: student.goToSchool() or student.age
  • In Dart there is only as , not as? like Kotlin.
  • The * operator is also useful in case you need to generate a string of desired length. print('a' * 3); // Output là: aaa

3. If else

Just like Kotlin

Or in the case in the { } block there is only one statement:

4. For loop

Kotlin seems to be easier to remember syntax. And Dart is a bit hard to remember, right? But don’t worry, because in part 2 I will introduce lambda expressions (lambda expressions), much easier to use than this. The variable list in the image is faulty because I have not declared that list variable. Through part 2, I will introduce the List type as well.

5. While loop

Similar to Kotlin:

while

do-while

6. Break and continue

The use of break and continue same as Kotlin.

7. Switch and case

It is when in Kotlin

8. String template, String raw

The String template is identical to Kotlin. It also uses $a and ${a.func}

String raw is different from Kotlin, Kotlin uses """ """ tripple quote, and Dart uses double quote " " .

Notice, I didn’t use escape before the ' character. That is, if you print the above sentence using single quotes, it must look like this:

9. Exceptions

Like Kotlin, all Exceptions in Dart are unchecked exceptions, meaning you won’t be forced to try catch catch any line of code.

Dart provides two classes, Exception (which is the parent class of all exceptions) and class Error . Exception and Error no one inherits anyone. These 2 guys are completely different in nature. If Exception are unforeseen exceptions and should try/catch these in case of unwanted cases. If your app encounters an Error , it proves that your code has failed, the code is bad, the code is error and you must fix it, not try/catch .

The way to throw an Exception is the same as that of Kotlin:

In addition, you can throw an exception regardless of the class:

10. try / catch / finally

Relatively similar to Kotlin, there are only 2 differences:

  • How to catch a specific exception: on FormatException catch (e) { }
  • To throw an exception back, Dart gives us the keyword rethrow

11. Null type

Class Null is very special, it has only one instance which is null . That’s why comparing nulls in Dart will have two types like this.

or

When declaring a variable that has no initial value, the variable will be of type Null , that’s why in lesson 1 I said that if there is no initial value, all variables have a default value of null

Output:

12.The 3-star prince

Just like Java. This operator does not have Kotlin.

Explanation: If variable isPublic == null then variable visibility is assigned the value 1 , otherwise it is assigned 0

13. The null-aware operator

Operator ??

Output:

This operator is similar to the Elvis ?: Operator in Kotlin. That means if x is null then print the backside

Operator ?? =

Output:

If the variable x is null , assign the value by the back side to x

Operator ?.

?. Used to check null before calling a function or property like Kotlin. For example student?.goToSchool() or student?.age .

14. List and Array Types

In Dart it combines the Array vs List type together with a type called List . The syntax for creating a List is [value, value, value, ...]

The add, set, remove, and get functions

Collection if, collection for

This collection if is not supported by Kotlin.

This collection for Kotlin also has no support.

Output:

It listOfStrings all elements in list listOfInts and then listOfStrings to listOfStrings list

The spread operator (spread operator and null-aware spread operator)

This spread ... operator Kotlin does not have, it helps me addAll a list B to a list A quickly.

Similar to null-aware spread operator ...? the same is true but differs in that it will check null before addAll.

15. Immutable and Mutable List

Actually, in Dart there is no concept of Immutable List. It just has a type called List , which allows this type of add , set and remove elements from the list, it is a mutable type. However, there are also 3 ways to circumvent the law to create an immutable list: v

Method 1 : Use List.unmodifiable pass a variable of type List

If immutable list Kotlin it will error compile right line of code a.add(3) and of course you will not be able to run the program, then at Dart is a little different, it doesn’t report compile error but when run it will error at Runtime:

In addition to the add function, the set, remove function will also have an exception:

Method 2 and 3 : use keyword const

16. Set

Set contains non-duplicate elements. The syntax for creating a Set is {value, value, value, ...}

Set also support spread operator ... and the null-aware spread operator ...?

17. Map

The syntax for creating a Map is: {key: value, key: value, key: value, ....}

Note : Since both Set and Map use { } so when we write code like this it will infer the type of Map :

If we want to create an empty Set we can do the following:

Conclude

So we have come more than half the way. Hope you guys continue to watch the sequels

Reference: https://dart.dev/guides

Read on to Part 3: Quickly Learn Dart (Flutter) with Kotlin (Part 3)

Share the news now

Source : Viblo