Dart Cheat Sheet

- Developer Tools -
Dart Cheat Sheet

What`s inside

A complete Dart cheat sheet for everyday use.
(from beginners to advanced users).

About the Content

If you are not an every day Dart person…. or
If you forgot a syntax that you once knew … or
If you want to take a glimpse of how things are done in Dart … or

I think that is too many or’s already.

Introduction

  • Made by Google
  • Object Oriented
  • Single Threaded
  • ⇒ Open Source
  • Dart Native for mobile, command-line, server-side
  • Dart Web for web apps

.

  • Type System
  • Classes (Single inheritance)
  • Interfaces (Multiple interfaces)
  • Concurrency (with isolates)
  • Collections (Heterogeneous by default)
  • Generics
  • Optional Typing
  • Async Await
  • Productive development (Hot reloading)

.

Tools

.

Compilers

  • dart2aot, dart to native x64 machine code
  • dartdevc (development time compiler), used only for web dev
  • dart2js (production time compiler), dart to javascript

.

Command Line

helloWorld.dart main() { print(“Hello World !!”); } . Terminal dart helloWorld.dart
> Hello World !!
.
Get all dependent packages. pub get . Upgrade packages to newer version. pub upgrade . Build the application pub build . Get commandline help pub help .

Data Manipulation

Type Conversion

.

Definite Loop

for loop

.

Definite Loop

for-in loop

.

Indefinite Loop

while loop

.

Indefinite Loop

do-while loop

.

break statement: Take the control out of a construct

.

continue statement: Skip a statement in the iteration

Label with Break . Label with Continue .
if statement . If…Else statement . else…if ladder . switch…case statement .

Functions

Functions in Dart .
To specify optional positional parameters, use square [] brackets. .
The parameter’s name must be specified while the value is being passed. Curly brace {} can be used to specify optional named parameters. .
Assigning parameter values by default. .
A concise mechanism to represent functions. .

Classes & Objects

Class declarations are themselves interfaces in Dart. .
A class in terms of OOP is a blueprint for creating objects. It encapsulates data for the object. .

Class Inheritance lets us create new classes (child/sub classes) from an existing class (base/parent/super class).

.

Normal Constructor . Named Constructor . Factory Constructor .

Variables And Data Types

Indentifiers & Operators

Identifiers are names given to elements in a program like variables, functions etc.

.

Arithmetic Operators .
Equality And Relational Operator .
Assignment Operators .
Logical Operators .
Bitwise and Shift Operators .
Every time a function is called, a reference to the object is required. The Cascade Operator can be used as a shorthand in cases where a sequence of invocations are required. .

Variables And Data Types

Data Types

Everything you can place in a variable is an Object. Numbers, functions, and null are all objects. .

Numbers in Dart

.

String is a sequence of UTF-16 code units

.

Booleans in Dart

.

– A rune is simply an integer representing the Unicode code point.

.

Enums are used for defining named constant values

.

Collection

In Dart, Array is represented in the form of a List

.

A Map is a dynamic collection of key value pairs.

.

A collection of objects in which each object can occur only once. .
A hash table based implementation of Map. .
An unordered hash-table based Set implementation. .
A collection that can be manipulated at both ends.

Useful for first-in, first-out collection

.

More

If you never intend to change a variable, use final or const.
final variable can be set only once
const variable is a compile-time constant (implicit final)

.

Enforces a restriction on the data type of the values that can be contained by the collection. .

Dart built-int exceptions:

IntegerDivisionByZeroException: When a number is divided by zero.

IOException: Base class for all Inupt-Output related exceptions.

DeferredLoadException : Thrown when a deferred library fails to load.

FormatException: Thrown when a string or some other data does not have an expected format and cannot be parsed or processed

IsolateSpawnException: Thrown when an isolate cannot be created.

Timeout: Thrown when a scheduled timeout happens while waiting for an async result

.

A function-type alias which can be used as pointer to specify a function signature that we want specific functions to match.

.

A means for getting a value sometime in the future.

.