Dart: Getting Started Guide 2023

Dart and Flutter are one of the most emerging technologies and it’s impossible for us to ignore such type of impressive technology. World’s growing Framework for Cross-platform app development (Flutter) is built using Dart. So in this post, we brought a complete Dart introduction for you. Let’s get started and dive into interesting content.

Also Read: What is Node.JS?

What is Dart?

Dart is open source object-oriented programming language that Google developed. It is the only programming language behind the world’s most trending and emerging app development framework called Flutter.

Recently it has launched the concept of null safety which had made this programming language much more powerful and secure.

If you want to try Dart without downloading then you can follow this link.

How to install Dart?

It is required to install Dart into the system to test it out locally. Below is a step-by-step guide on how to install the Dart in several operating systems.

Install Dart on Windows

  1. Make sure that you have chocolatey installed on your Windows machine.
  2. If it is not installed then click on this link to see the installation guide.
  3. After successfully installing chocolaty simply run the below command in the command prompt and Dart will be automatically installed in your system.
choco install dart-sdk

Install Dart on Linux

Linux is known for its easy-to-use package managers and those package managers make installation of Dart very easy. Below are the commands that you just need to copy and paste into your terminal to install Dart.

 $ sudo apt update

$ sudo apt install apt-transport-https

$ wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo gpg --dearmor -o /usr/share/keyrings/dart.gpg

$ echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | sudo tee /etc/apt/sources.list.d/dart_stable.list

$ sudo apt update

$ sudo apt install dart

We’ll be using Visual Studio Code to write our programs and with it, a Code Runner plugin that helps us run code with a single click of a button.

How to run Dart file in VS Code

To run a dart file in Visual Studio code you just need to open any folder inside the VS Code at them create any file with the .dart extension. As we are doing dart programming language we need to specify file extensions as “.dart”.

Once you have created the file, you can simply write your code in it and press F5 or click on the run button in the top right corner of VS Code.

Dart Hello World Program

Let’s see a Hello World program to understand how execution takes place in Dart.

void main() {
  print('Hello world');
}

In the above code, there is a main function which is the entry point of the Dart program and hence everything inside the main function will be executed when the program runs. Inside the main function, there is a print statement in which we have written ‘Hello World’ hands it will be automatically printed on the screen.

Output:

Hello World

Comments in Dart

Comments are those quotes that are not executed during the program execution but are just returned by the developer for their knowledge of something.

Example:

void main() {
  // print('Hello world');
}

The above code will not produce any output.

Output:

Nothing will be shown on the screen.

Variables in Dart

Variables are something that is used to store data. Suppose you want to store the name of user four for later use in your program then we will use a variable for this.

We can declare variables by using two keywords: –

  1. var
  2. const

Example 1: Using the var keyword

void main() {
  const name = 'Sushant';
  print(name);
}

Output:

Sushant

Example 2: Using the const keyword

void main() {
  final name = 'Sushant';
  print(name);
}

Output:

Sushant

Difference between var and const

There is very little difference between the var and const keywords and you should know it. The difference is that variables declared using the var keyword can be assigned new values later in the program. But it is not the same for the case of const. It means a constant value that can’t be changed later.

Data Types in Dart

Data types are used to explicitly specify what type of value or data a particular variable can have. If any other type of value is passed, it will throw an error. There are several data types in the dart programming language.

  1. String
  2. int
  3. bool
  4. List

Let’s deep dive into Data Types

String: – A string is something that is used to store a combination of alphabets or you can say a word. Suppose you want to store your name in any variable then you will use a string for this.

int: – If we need to store any type of number then we use the int data type. The limitation of this data type is that it can store only whole numbers and to store decimal numbers you will need to use another data type called double.

bool: – Sometimes we need to store either true or either false so that we can use them later for condition checking. Do not store true and false as a string. Because we have a dedicated data type for that and it’s bool. Just use it to store true or false.

List: – To save more than one item of the same data type in one variable we can use a list.

Example:

Below is the code to demonstrate all the data types.

void main() {
  String name = 'Sushant';
  int age = 19;
  bool isMarried = false;
  List favouriteSingers = ['Eminem', 'Sidhu'];

  print(name);
  print(age);
  print(isMarried);
  print(favouriteSingers);
}

Output:

Sushant
19
false
[Eminem, Sidhu]

Dart Control Flow Statement

Most of the time in programming you need to make decisions dynamically based on the situation. For example, if a user is already logged in then he will be directed to their account else he will be asked to log in first this is called decision-making. We use if and else keywords for this.

Example:

void main() {
  String name = 'Sushant';
  bool isMarried = false;
  
  if(isMarried == false){
    print('Sushant is not married');
  } else {
    print('Sushant is married');
  }
}

There are two keywords if and else which are used to check conditions. In the above code, there is a bool storing the value false. Then in the next line, I started an if-else block and checked if the bool is false or true. If it is false then I executed a print statement and if it is true then I executed the respective print statement.

Output:

Sushant is not married

Why is everything inside the main?

Now you have written basic programs but one thing that you might not notice is why everything is written inside the main. The reason for that is the main function is the entry point of your dart application and once the app starts the main function is the only function that is going to execute first.

Summary

We hope that this post was helpful to you. This was just introductory content for Dart, in our upcoming posts, we will cover Dart and Flutter-related topics deeply. We will also be going to develop apps from scratch. So stay tuned to the website and keep visiting us for the latest content.

Reference

https://dart.dev/

Sushant Dhiman
Sushant Dhiman
Articles: 14