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

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 2, you can read it again here

2. Function

  • Actually, in both Kotlin and Dart there is no concept of a function with no  return type. All functions have return but if we don’t use return then both Kotlin and Dart will return implicitly. Kotlin will return an implicit value of Unit and Dart will return null. I comment on the code so it is easy to compare functions with return and not return hope people don’t misunderstand
  • If you look at it, Dart’s function is very similar to Java

3. Single-Expression function

  • Dart uses arrow syntax  => to write functions that have only 1 expression quickly (similar to Kotlin)
  • Dart is more sophisticated than Kotlin in that it removes both the function’s return type and the data type of the parameter lun. Kotlin can only remove the return type. However, this coding is discouraged in Dart (you see it that warning). Dart wants you to, when declaring a function, to fully declare the return type and type of the parameters (if any).

Or you can use it to  throw Exception:


4. Local funtions

Just like Kotlin, Dart can also declare a function inside one function and the output of one function can also be the input of the other.

5. Named parameters

  • Like Kotlin, Dart also supports named parameters that have only one difference in syntax: Dart requires parameters to be in {}
  • Between the param name and the argument, Kotlin uses the  = (sign: bbb = 1), while Dart uses the : (VD: bbb: 1)
  • Once you have used the named parameters feature in the function (ie, with the  { } As mentioned above, Dart is required to specify the name of each param when calling the function. And Kotlin is smarter, it will understand itself  a how much longer bbb what is it. As you can see in the first case, Kotlin doesn’t report an error and Dart reports an error.

6. Positional parameters

This term is a bit strange to Kotlin, the parameters are located in  [ ] then, when calling the function, it is not necessary to pass in. In other words, it can be passed in, it can not be transmitted. If not passed, it will have the default value of null

Kotlin doesn’t have it, but it can also be done using the default arguments ha

7. Default arguments

Like Kotlin, Dart also supports default arguments

The default arguments are also used in positional parameters

8. Function reference

If so, Kotlin needs an operator  :: to prefix the function, Dart doesn’t need anything.

The output of both Kotlin and Dart is:

9. Anonymous function, lambda expression

In Kotlin it clearly distinguishes between anonymous function and lambda expressions, Dart considers these two guys as one.


For example a for loop is written as lambda:

Chú ý: Kotlin has it, but Dart doesn’t. I have to name it as I named it here element

10. Function type

Dart is actually an object-oriented language, so even functions are objects and have a type  Function. So we can assign a function to a variable in Dart. This is the same as Function Type in Kotlin.

Both Kotlin and Dart print the output:

11. High order funtion

The function that accepts the parameter is the function

I wrote a comparison example for you to easily understand the writing of Dart

Or you also use it for callback purposes like this

Output of both Kotlin and Dart:

The function returns a function

In Dart a function can also return a function.


This section is only about functions that are so much. Seeing Dart’s idea as well as Kotlin does. Hope you guys continue to watch the sequels

Refer:  https://dart.dev/guides

Share the news now

Source : Viblo