Dart Basics

Get an introduction to the basics of the Dart programming language, used for development with the Flutter SDK for mobile, web and beyond. By Jonathan Sande.

4.6 (19) · 2 Reviews

Download materials
Save for later
Share
Update note: Jonathan Sande updated this tutorial for Dart 2.12. Joe Howard wrote the original.

Flutter is an exciting UI toolkit from Google that lets you write apps for different platforms including iOS, Android, the web and more, all using one codebase. Flutter uses the Dart language.

If you’re not familiar with Dart yet, this tutorial will introduce you to its basic concepts and show you how it’s similar to other programming languages you may already know.

Over the course of this tutorial, you’ll get an introduction to Dart basics such as:

  • Variables, data types, and operators
  • Conditionals and loops
  • Collections
  • Functions

By the time you’re done, you’ll be ready to dive right into Flutter development using Dart.

Getting Started

Download the example code by clicking the Download Materials button at the top or bottom of the page. You won’t be building a sample project in this tutorial, but you can use the coding examples as a reference.

You can either paste the code from main.dart into DartPad or use the Dart SDK to run the file.

To get started quickly, your best bet is to use the open-source tool DartPad, which lets you write and test Dart code via a web browser:

DartPad editor, annotated to show the location of each element below

DartPad is set up like a typical IDE. It includes the following components:

  • Editor pane: Located on the left. Your code will go here.
  • RUN button: Runs code in the editor.
  • Console: Located on the upper-right, this shows output.
  • Documentation panel: Located on the bottom-right, this shows information about code.
  • Samples: This drop-down shows some sample code.
  • Null Safety button: Use this to opt in to Dart’s new sound null safety feature.
  • Version information: At the very bottom-right, DartPad shows which versions of Flutter and Dart it’s currently using.

If you prefer, you can install the Dart SDK locally on your machine. One way to do so is to install the Flutter SDK. Installing Flutter will also install the Dart SDK.

To install the Dart SDK directly, visit https://dart.dev/get-dart.

Why Dart?

Dart has many similarities to other languages such as Java, C#, Swift and Kotlin. Some of its features include:

  • Statically typed
  • Type inference
  • String expressions
  • Multi-paradigm including object-oriented and functional programming
  • Null safe

Dart is optimized for developing fast apps on a variety of platforms.

Core Concepts

Dart programs begin with a call to main. Dart’s syntax for main looks similar to that of other languages like C, Swift or Kotlin.

Clear out all the code in the default DartPad and add main to the editor:

void main() {
  
}

You’ll see there’s a return type before main. In this case, it’s void, meaning that main won’t return anything.

The parentheses after main indicate that this is a function definition. The curly brackets contain the body of the function.

Inside main, you add the Dart code for your program.

Next, you’ll learn more about the following core concepts:

  • Variables, Comments and Data Types
  • Basic Dart Types
  • Operators
  • Strings
  • Immutability
  • Nullability
  • Condition and Break
  • For Loops

It’s time to dive in!

Variables, Comments and Data Types

The first thing you’ll add to main is a variable assignment statement. Variables hold the data that your program will work on.

You can think of a variable as a box in your computer’s memory that holds a value. Each box has a name, which is the name of the variable. To denote a variable with Dart, use the var keyword.

Add a new variable to main:


var myAge = 35;  

Each Dart statement ends in a semicolon, just as statements do in C and Java. In the code above, you’ve created a variable, myAge, and set it equal to 35.

You can use the built-in print in Dart to print the variable to the console. Add that call after the variable:


print(myAge); // 35

Click RUN in DartPad to run the code. You’ll see the variable’s value, 35, prints in the console.

First Dart output

Comments

Comments in Dart look just like those in C and in other languages: Text following // is a single-line comment, while text within /* ... */ is a multi-line comment block.

Here’s an example:


// This is a single-line comment.

print(myAge); // This is also a single-line comment.

/*
 This is a multi-line comment block. This is useful for long
 comments that span several lines.
 */

Data Types

Dart is statically typed, meaning that each variable in Dart has a type that must be known when you compile the code. The variable type cannot change when you run the program. C, Java, Swift and Kotlin are also statically typed.

This contrasts with languages like Python and JavaScript, which are dynamically typed. That means variables can hold different kinds of data when you run the program. You don’t need to know the type when you compile the code.

Click myAge in the editor window and look in the Documentation panel. You’ll see Dart inferred that myAge is an int because it was initialized with the integer value 35.

If you don’t explicitly specify a data type, Dart uses type inference to try to determine it, just like Swift and Kotlin do.

Type Inference

Dart also uses type inference for types other than int. Enter a variable, pi, equal to 3.14:


var pi = 3.14;

print(pi); // 3.14

Dart infers pi to be a double because you used a floating-point value to initialize it. You can verify that in the Dart info panel by clicking pi.

double data type inference

Alternatively, instead of using type inference, you can declare the type.

Basic Dart Types

Dart uses the following basic types:

  • int: Integers
  • double: Floating-point numbers
  • bool: Booleans
  • String: Sequences of characters

Here’s an example of each Dart type:

Dart Data Types

int and double both derive from a type named num. num uses the dynamic keyword to mimic dynamic typing in the statically typed Dart.

Do this by replacing var with the type you want to use:


int yourAge = 27;

print(yourAge); // 27

The Dynamic Keyword

If you use the dynamic keyword instead of var, you get what’s effectively a dynamically typed variable:


dynamic numberOfKittens;

Here, you can set numberOfKittens to a String using quotes. You’ll learn more about the String type later in the tutorial.


numberOfKittens = 'There are no kittens!';

print(numberOfKittens); // There are no kittens!

numberOfKittens has a type, since Dart has static typing. But that type is dynamic, which means you can assign other values with other types to it. So you can assign an int value below your print statement.


numberOfKittens = 0;

print(numberOfKittens); // 0

Or, if you have a kitten in Schrödinger’s box, you could assign a double value:


numberOfKittens = 0.5;

print(numberOfKittens); // 0.5

Schrödingers Cat

Click Run to see the three different values for numberOfKittens printed in the console. In each case, the type of numberOfKittens remains dynamic, even though the variable itself holds values of different types.

Dart dynamic data type