ZetCode

Introduction to Dart

last modified December 22, 2020

This is introduction to the Dart programming language. Dart first appeared in 2011; it is developed by Google. The website of the language is https://dart.dev/.

An online development environment is available at https://dartpad.dev/.

Dart

Dart is a client-optimized language for fast applications on any platform. It is used to build mobile, desktop, server, and web applications. It is a programming language that is easy to learn and has a familiar syntax. It is optimized for building user interfaces.

Dart is an object-oriented, garbage-collected language with C-style syntax. It can compile to either native code or JavaScript.

Dart is the language of the Flutter toolkit.

Dart installation

Dart is very easy to install.

$ sudo apt-get install dart

On Debian-based Linux distributions, we can install Dart with the above command.

$ dart --version
Dart SDK version: 2.10.4 (stable) (Unknown timestamp) on "linux_x64"

After successfull installation, we can check the installed version of Dart with the --version command.

Dart has a good extension for VS Code called Dart; it contains language support and debugger for Visual Studio Code.

Dart simple example

The following is a simple example in Dart.

simple.dart
void main() {
  print('First program in Dart');
}

The program prints a message to the console. Dart programs have the .dart extension. The main function is the entry point to the program. The function name is preceded with the void keyword indicating that the function does not return anything.

The body of the function is enclosed in a pair of curly brackets. The print function displays a message in console. The statements are terminated with a semicolon.

$ dart simple.dart 
First program in Dart

We run the program.

Dart variables

Variables store references to values.

variables.dart
void main() {
  String name = 'John Doe';
  int age = 34;

  print('$name is $age years old');
}

In the example, we have a string and a integer variable. The names of the variables are preceded with String and int data types.

String name = 'John Doe';

We can create string literals both with single and double quotes.

print('$name is $age years old');

Dart supports variable interpolation in strings. The variables preceded with the $ character are evaluated to their values inside strings.

$ dart variables.dart 
John Doe is 34 years old

Dart user input

The dart:io library provides file, socket, HTTP, and other I/O support for non-web applications.

user_input.dart
import 'dart:io';

void main() {
  stdout.write("Enter your name: ");
  var name = stdin.readLineSync();
  print('Hello $name\n');
}

The example prompts the user for his name and prints a message.

stdout.write("Enter your name: ");

We can use the stdout.write function to write to the console without a newline character.

var name = stdin.readLineSync();

We read the user input with stdin.readLineSync.

$ dart read_input.dart 
Enter your name: Peter
Hello Peter

This was an introduction to the Dart programming language.

List all Dart tutorials.