1. Introduction
Recently when I researched about Flutter, I found that Flutter uses the Dart programming language. With knowledge of Kotlin programming language, it only takes 1 day to learn Dart and I want to summarize this 1-day route and share it with you. If it took me a day to learn, I hope that after reading this series, you will be able to code Dart. In this series, I will focus on translating the code from Kotlin to Dart, how will Kotlin code write like this, how Dart will write and along with that I will show the features that in the Dart language have that Kotlin does not have. Of course, Kotlin also has features that Dart doesn’t, but those are not part of the purpose of this series so I won’t write them.
Of course, a prerequisite to understand this series is that you must have knowledge of Kotlin or Java or an object-oriented language.
Let’s begin!
2. Prepare your luggage
First we must have the environment for coding Dart. There are 2 paths for you to choose. One is to use IDE, you need to download, install, configure new types of code, this path is difficult but happy later. The other way is instant noodles: go to a Dart website to keep the code installed.
The road of suffering before happy after
Here I will guide if you choose the path of suffering, then choose the IDE to code. IDE can be IntelliJ IDEA IDE, Visual Studio Code, …. I choose IntelliJ IDEA IDE. You can download it here . Next we need to install the Dart plugin on IntelliJ IDEA IDE. You choose File / Setting menu.
Next we need to download and install the Dart SDK (or if you already have the Flutter SDK installed, in the Flutter SDK it also contains the Dart SDK). Click here for instructions on downloading and installing the Dart SDK: https://dart.dev/get-dart
Ok, let’s start creating your first project: choose File / New / Project menu
You can see that your Dart SDK path is not in the Flutter SDK (if you have installed Flutter SDK, the path of the Dart SDK in the Flutter SDK in the window is as follows: C:flutterbincachedart-sdk
) . The reason I download my own Dart SDK is because I want the latest Dart version code to be 2.9.0 (stable release on August 5, 2020, Wow! It’s 4 days from the time I wrote this article, the new item is hard. In the Flutter SDK, Dart is only at version 2.8.4
The second reason I choose Dart sdk version 2.9.0 or higher is to experience the Null Safety feature (similar to Kotlin), and the old versions, Dart do not have this feature. This version 2.9.0 is not included in the Flutter SDK. In other words, the Flutter project has not yet fought this Null Safety feature. Even if it works, Google also advises not to enable the Null Safety feature in production because it is only an experimental feature, there will be a lot of risk when using it. Although I use ver 2.9.0 but I did not enable the experiment feature of Dart, the code is the same as ver 2.8.x only. Throughout this article I will not enable the experiment feature because I want to finish learning so I have to use it immediately to fight Flutter. Therefore, you do not have to install the latest version 2.9.0, you can still use the 2.8.x version if you want. Of course, I also don’t want to miss an important feature like Null Safety so I will enable the experiment of Dart and introduce the Null Safety feature at the end of this article.
The way of instant noodles
Up to 2 websites, one with integrated Null Safety: https://nullsafety.dartpad.dev/
This second site will not have the Null Safety feature: https://dartpad.dev/
If you want to test the examples in this article, I recommend using the second site that does not have Null Safety feature. Since throughout this article I will not enable the experiment feature, if you use the first website to run the code in this article, it may cause unwanted side effects !.
OK! Enough luggage. Let’s get started!
3. Name the source code file and folder / package
- The package / folder names of Kotlin and Dart are both written in lower case, but unlike Kotlin, naming package / folder without underscore
_
, Dart allows the use of_
- Dart’s source code file extension is .dart, and the filename convention is different from Kotlin. Kotlin uses
UpperCamelCase
(e.g., MyClassName.kt), Dart useslowercase_with_underscores
(e.g. my_class_name.dart) and is only allowed to use Latin charactersaz
, digits0-9
, underscrore_
and cannot be captured. beginning by a digit.
In a nutshell: Naming the source code file and the folder / package in Dart both use stype:
lowercase_with_underscores
4. The main () function
Like Kotlin, main()
is a function that will run first when the project is run. The only difference here is the syntax. Kotlin uses fun
and Dart uses void
(pretty much like Java). There are a lot of things that Dart is pretty much like Java, so if you have already learned Java then learning Dart will be pretty easy. However, the main character in my post is Kotlin, so I won’t talk much about Java.
5. The print () function and the sign ;
the legend come back
- Dart uses the print () function to print a newline equivalent to the println () function in Kotlin
- String in Kotlin uses quotes
" "
and Dart uses single quotes' '
- Expressions in Kotlin do not need a sign
;
but Dart needs a sign;
the legend at the end of each expression.
6. Basic built-in data types
Integer type
Kotlin has 4 types of integers:
And Dart has only one type of integer, int
(64 bits), equivalent to the Long
type in Kotlin. Note that the type of int
is written normally like Java
Real number type
Kotlin has two types of real numbers: Float
(32 bits) and Double
(64 bits), while Dart has only one type of real number, double
(64 bits) and also lowercase like Java.
Boolean type
In Kotlin it’s Boolean
and in Dart it’s bool
String type
Both Kotlin and Dart use String
. However, in Dart there is no Char
type
To summarize the basic types:
7. Comment
Comments in Dart are similar to Kotlin: // nội dung comment
or /* nội dung comment */
8. Declare variables and constants
In Dart there are 4 ways to declare variables:
- Two ways to declare variables both get and set are:
{kiểu dữ liệu} {tên biến} = {giá trị khởi tạo}
and
var {tên biến} = {giá trị khởi tạo}
However, using the keyword var
is encouraged because it helps support his head slightly to think keyword data type when you declare a variable, variable datatype want anything blueberries var
all. If you are curious to know more about why, you can read it here .
- Dart can also predict the Type Inference similar to Kotlin
- To declare the variable read-only (get only), we use the syntax:
final {tên biến} = {giá trị khởi tạo}
- To declare a constant, we use the syntax:
const {tên hằng} = {giá trị khởi tạo}
- Variable naming method uses
lowerCamelCase
(eg isMyDog) similar to Kotlin, but naming constants is different. Kotlin names constants as all caps and_
(eg:CONNECTION_TIMEOUT
), while in Dart, named constants also use thelowerCamelCase
type similar to variable names (egconnectionTimeout
).
9. Dynamic type
This is a unique type that Kotlin doesn’t have, which is the dynamic
type. This type allows flexibility of the data type, in other words a variable that can change the data type. It’s amazing: v
1 2 3 4 5 6 |
Output: 3 3.5 true Minh |
However, an interesting discovery is that var
also capable of converting data types like dynamic
10. Type num
One more unique type that Kotlin doesn’t have anymore is num
. This type is both an integer and a real number. Magic episode 2
- The
runtimeType
function is available in all objects to print the data type - It can be seen that this
num
type is similar todynamic
except thatnum
can only convert between two types,int
anddouble
- Of course when users type
num
to use way{kiểu dữ liệu} {tên biến} = {giá trị khởi tạo}
then this viscous than gasoline, thevar
it guess what style is. In Dart, there is novar
syntax with type like Kotlin.
However, final
with const
can be accompanied by type but will be warning because of redundant code
11. Type of Object
Type Object
in Dart is like Any?
in Kotlin. That is the grandfather of all types (root class). In other words, all types of int
, double
, String
, Student
, List
, … are children of class Object
. Because of that, all data types in Dart are object types including int
, double
or bool
. Hence the default value of all data types is null
. For example:
1 2 3 4 5 |
void main() { int a; // không có giá trị khởi tạo print(a); // nên nó sẽ in ra giá trị default là: null } |
Conclude
Initially, I planned to write in one article together. However, the post has only been halfway so long, so I will split up many parts. The first part is just temporary write so much. Hope you guys continue to watch the sequels
Reference: https://dart.dev/guides
Read on to Part 2: Quickly Learn Dart (Flutter) with the Kotlin Language (Part 2)