Dart Introduction

What is Dart?

The Dart programming language is to build mobile, desktop, server, and web applications on cross platforms.

It's a client-optimized, object-oriented, class-based, garbage-collected programming language with C-style syntax.

  • It is designed by Lars Bak and Kasper Lund.
  • Developed by Google.
  • Released under BSD license.
  • Dart can compile to either native code or JavaScript.

Main features of Dart

  1. Mobile app development
  2. Command-line applications
  3. HTTP clients & servers
  4. Dart Web platform

Mobile app development

The Flutter framework powered by the Dart platform is used for developing mobile apps.

Main advantages are Dart VM and Dart AOT

  1. The Dart VM for its instant hot reload developer cycle.
  2. The Dart AOT native code compiler for creating fast production code.

Command-line applications

Dart also supports command-line applications.

Dart provides resources via packages (e.g. dart.io), that are used in most command-line applications, including the standard input, output, error, streams, command-line arguments, files and directories, and more.

HTTP clients & servers

Dart also supports building HTTP clients & servers applications.

In Dart, the dart:io library contains the classes and functions to write HTTP clients and server applications.

In addition, the http_server package contains some higher-level classes that make it easier to write clients and servers.

Dart Web platform

Dart supports the web as one of its core platforms.

Dart-to-JavaScript compilers are available both for development and for production.

In addition to compilers, the Dart web platform provides.

  1. Core libraries,
  2. Access to the DOM (Document Object Model),
  3. And interoperability for calling JavaScript from Dart.

To build a robust web application, consider using Dart web with a higher-level web application framework like AngularDart.

Note: Many apps that support both web and mobile are built using Flutter and Flutter web support.

DartPad

  1. Navigate to the https://dartpad.dev.
  2. Type or paste code in the code editor.
  3. Click the run button to execute the code.

Basic Program

// Declare a function.
displayName(String name) {
  // Print to console.
  print('My name is $name');
}

// This is where the app starts executing.
main() {
  // Declare and initialize a variable.
  var fullName = "Rock Star";

  // Call a function.
  displayName(fullName);
}

Here’s what this program uses that applies to all (or almost all) Dart apps.

Single-line comment: A single-line comment // ignored by the Dart compiler.

// Declare a function.

Function:

A function declaration named as displayName, and String static type defined as function parameter.

displayName(String name) {}

print() function: A handy way to display output. The top-level print() method takes a single argument (any Object) and displays that object’s string value (as returned by toString()) in the console.

print('My name is $name');

String interpolation: Including a variable $name using String interpolation inside of a string literal.

'My name is $name'

main() function: A top-level function where app execution starts.

main() {}
  1. var keyword: A way to declare a variable without specifying its type.
  2. variable: fullName a variable name.
  3. String literal: Rock Star a string literal.
var fullName = "Rock Star";