ZetCode

Introduction to Dart

last modified January 28, 2024

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.

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

The program prints a message to the console. Dart programs have the main.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 main.dart
First program in Dart

We run the program.

Dart variables

Variables store references to values.

main.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 main.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.

main.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 main.dart
Enter your name: Peter
Hello Peter

Source

Dart Guides

This was an introduction to the Dart programming language.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Dart tutorials.