Skip to content

Dart Language Basic Teaching Note

Last Updated on 2021-10-12 by Clay

In the past, a teacher had a whim to ask each student to make a mobile application as the final project; it was also at that time, my friend recommended me to learn how to use the Flutter framework to develop mobile apps.

In fact, you can really develop a simple small app by reading Flutter’s official guide.

However, as the functions I want to develop continue to upgrade and become more and more complex, my unfamiliarity with the Dart language of Flutter used has been exposed.

I need to complete a lot of programming logic that can be completed quickly in other languages, but I need to query Dart’s syntax repeatedly.

That’s why I want to write this article to help myself review the basic syntax of Dart.


Introduction of Dart

Dart is a programming language developed by Google. It was a scripting language used in browsers and an object-oriented language similar to JavaScript. (However, Dart’s popularity is significantly lower than that of JavaScript and Golang)

In recent years, due to the development of the Flutter development kit, some friends have begun to learn Dart, which is a relatively smooth time for the development of Dart.

It is worth mentioning that there are two ways to execute Dart:

  • Execute on a native virtual machine
  • Convert Dart code to JaveScript

If you want to test some simple code recorded in this article, it is recommended to use DartPad, an online tool to execute.

The screen of DartPad is as follows:

On the left is an editor, you can write down the Dart code and after clicking the blue RUN button, the execution result will be displayed in the console block on the right.


How to write Dart code

I recorded Dart language syntax in the following small units:

  • Hello World
  • Comment
  • Variable and Data Type
  • Flow Control
  • Function
  • Class
  • Import


Hello World (& how to print)

To learn a new programming language, the first time you need to print Hello World.

void main() {
  print("Hello World!");
}


Output:

Hello World!


In Dart, main() is the entry point of program, from where the program is executed; and if you want to print something, you can use print() function.

By the way, it is okay to write 'Hello World!' or "Hello World!".


Comment

/*
void main() {
  print("Hello World!");
}
*/

void main() {
  // This is a one-line comment
  print("Hello World!");
}


Output:

Hello World


Comments are similar to C language. There are single-line comment and multi-line comments.


Variable and Data Type

Before to define a variable, I want to record the basic data types of Dart:

Data TypeKeywordDescription
Numbernum, int, doubleNumerical types are divided into integers and floating-point numbers
StringsStringString (character sequence)
Booleansbooltrue & false
ListsListOrdered array of objects
MapsMapKey-Value pairs
String name = "Clay"; // Strings
int age = 18;      // Numbers 
bool fat = false;   // Boolean
List hobbies = [    // Lists
  "eating",
  "playing",
  "coding",
];

void main() {
  print("name: ${name} (${name.runtimeType})");
  print("age: ${age} (${age.runtimeType})");
  print("fat: ${fat} (${fat.runtimeType})");
  print("hobbies: ${hobbies} (${hobbies.runtimeType})");
}


Output:

name: Clay (String)
age: 18 (int)
fat: false (bool)
hobbies: [eating, playing, coding] (JSArray<dynamic>)


In addition to specifying the data type of variable, you can also use var to declare the variable like JaveScript, and let the variable determine the data type according to the assignment.

We can rewrite the code above as:

var name = "Clay"; // Strings
var age = 18;      // Numbers 
var fat = false;   // Boolean
var hobbies = [    // Lists
  "eating",
  "playing",
  "coding",
];

void main() {
  print("name: ${name} (${name.runtimeType})");
  print("age: ${age} (${age.runtimeType})");
  print("fat: ${fat} (${fat.runtimeType})");
  print("hobbies: ${hobbies} (${hobbies.runtimeType})");
}


This way of writing does not affect the output result.


Flow Control

For loop

First of all, let’s look at how to write the for loop in Dart, which is very similar to C.

void main() {
  for (int i=0; i<10; ++i) {
    print(i);
  }
}


Output:

0
1
2
3
4
5
6
7
8
9



While loop

Let’s look at a version of the while loop.

void main() {
  int i = 0;
  while (i < 10) {
    print(i);
    ++i;
  }
}


Output:

0
1
2
3
4
5
6
7
8
9



if-else + continue & break

Simply put, it is similar to other programming languages:

  • if: is the first judgment condition
  • else if: is the Nth (N >= 2) judgment condition
  • else: the block executed when all conditions are not met

In addition, in the loop block:

  • continue: Skip the part executed below and start the next loop directly
  • break: Jump out of the loop and no longer execute the code in the loop

Let’s look at an example, let i count from 0 to 50, do not print before 20, print out from 20 to 30, and then jump out of the loop at 31.

void main() {
  int i = 0;
  while (i < 50) {
    ++i;
    if (i < 20) {
      continue;
    }
    else if (i == 31) {
      break;
    }
    
    print(i);
  }
}


Output:

20
21
22
23
24
25
26
27
28
29
30



Function

int sum(int a, int b) {
  return a + b;
}


void main() {
  int a = 2;
  int b = 3;
  int c = sum(a, b);
  
  print(c);
}


Output:

5



Class

The introduction of Object and Class is skipped here; let’s take a look at an example of how to build a Class.

class human {
  String name;
  int age;
  bool fat;
  
  // Init
  human(String name, int age, bool fat) {
    this.name = name;
    this.age = age;
    this.fat = fat;
  }
  
  // Method
  void info() {
    print("My name is ${this.name}");
    print("I am ${this.age} years old.");
    
    if (this.fat == true) {
      print("I am a little fat. =(");
    }
    else {
      print("I am not fat!");
    }
  }
}


void main() {
  // Init
  var clay = human("Clay", 18, false);
  var atlas = human("Atlas", 25, true);
  
  // Describe
  clay.info();
  atlas.info();
}


Output:

My name is Clay
I am 18 years old.
I am not fat!
My name is Atlas
I am 25 years old.
I am a little fat. =(


This is an example I liked, packaging character information into a Class as an example.


Import

import "dart:math";               // Import Dart library
import "package:xxx/xxx.dart";    // Import Packages
import "PATH/TO/DART_FILE.dart";  // Import files



References


Read More

Leave a ReplyCancel reply

Exit mobile version