Dart Cheat Sheet

Static Methods

class Car {

static var MilesToKM = 1.60934;

static double kilometersToMiles(

double km) {

return km / 1.60934;

}

void accelerate() {}

void decelerate() {}

void stop() {}

void printSpeed() {}

}

Variables

int n1 = 5;

var n2 = 4;

// n2 = “abc”;

dynamic n3 = 4;

n3 = “abc”;

double n4;

String s1 = ‘Hello, world!’;

var s2 = “Hello, world!”;

Constants

const PI = 3.14;

final area = PI * 5*5;

Conditional Expressions

var grade = 3;

var reply = grade > 3 ? “Cool”:”Not

cool”;

var input;

var age = input ?? 0;

print(age);

Functions

void doSomething() {

print(“doSomething()”);

}

int addNums1(num1, num2, num3) {

return num1+num2+num3;

}

doSomething();

print(addNums1(1,2,3));

Arrow Syntax

void doSomethingElse() {

doSomething();

}

void doSomethingElse() =>

doSomething();

Optional Positional Parameters

int addNums2(num1, [num2=0, num3=0])

{

return num1+num2+num3;

}

print(addNums2(1));

print(addNums2(1,2));

print(addNums2(1,2,3));

Named Parameters

int addNums3({num1, num2, num3}) {

return num1+num2+num3;

}

print(addNums3(

num1:1,num2:2,num3:3));

Optional Named Parameters

int addNums4(num1, {num2=0, num3=0})

{

return num1+num2+num3;

}

print(addNums4(1));

print(addNums4(1,num3:2));

print(addNums4(1,num2:5,num3:2));

Parsing

var s1 = “123”;

var s2 = “12.56”;

var s3 = “12.a56”;

var s4 = “12.0”;

print(num.parse(s1));

print(num.parse(s2));

print(num.parse(s3));

print(num.tryParse(s3));

String Interpolation

var s1 = “Hello”;

var s2 = “world”;

var s3 = s1 + “, ” + s2;

var s = “${s3}!”;

print(s);

print(“Sum of 5 and 6 is ${5+6}”);

List (Arrays)

var arr = [1,2,3,4,5];

print(arr.length);

print(arr[1]);

arr[4] *= 2;

print(arr[4]);

arr.add(6);

print(arr);

List arr2;

arr2 = arr;

arr2[1] = 9;

print(arr);

print(arr2);

var arr3 = new List(3);

print(arr3);

arr3.add(5);

Map

var details = {“name”:”Sam”,

“age”:”40″};

print(details);

var devices = new Map();

var apple = [“iPhone”,”iPad”];

var samsung = [“S10″,”Note 10”];

devices[“Apple”] = apple;

devices[“Samsung”] = samsung;

for (String company in

devices.keys) {

print(company);

for (String device in

devices[company]) {

print(device);

}

}

Lambda Functions

var nums = new

List<int>.generate(10, (i) => i);

print(nums);

var odds = nums.where(

(n) => n % 2 == 1).toList();

print(odds);

var sum = nums.reduce(

(s,n) => s + n);

print(sum);

var prices = nums.map(

(n) => “\$$n”).toList();

print(prices);

Higher Order Functions

var names = [“Jimmy”,”TIM”,”Kim”];

names.sort(

(a, b) =>

a.toUpperCase().compareTo(

b.toUpperCase())

);

print(names);

names.sort((a,b) {

if (a.length > b.length)

return 1;

else

return -1;

});

print(names);

List bubbleSort(List items, bool

Function (int,int) compareFunction)

{

for (var j=0; j<items.length-1;

j++) {

var swapped = false;

for (var i=0;

i<items.length-1-j; i++) {

if (!compareFunction(items[i],

items[i+1])) {

var t = items[i+1];

items[i+1] = items[i];

items[i] = t;

swapped = true;

}

}

if (!swapped) break;

}

return items;

}

var nums = [5,2,8,7,9,4,3,1];

var sortedNums = bubbleSort(nums,

(n1,n2) => n1<n2);

print(sortedNums);

sortedNums = bubbleSort(nums,

(n1,n2) => n1>n2);

print(sortedNums);

Iterations

for (int i=0;i<5; i++) {

print(i);

}

var list = [1,2,3,4,5];

for (final i in list) {

print(i);

}

int i=0;

while (i < 5) {

print(i);

i++;

}

i = 0;

do {

print(i);

i++;

} while (i<5);

Class

class MyLocation {

}

var loc1 = new MyLocation();

MyLocation loc2 = new MyLocation();

Properties

class MyLocation {

var lat;

var lng;

final arrived = false;

}

loc1.lat = 57.123;

loc1.lng = 37.22;

// loc1.arrived = true;

var arr = loc1.arrived;

Methods

class MyLocation {

var lat;

var lng;

final arrived = false;

void someMethod() {

}

}

loc1.someMethod();

Constructors

class MyLocation {

var lat;

var lng;

final arrived = false;

MyLocation() {

this.lat = 0;

this.lng = 0;

}

MyLocation.withPosition(

var lat, var lng) {

this.lat = lat;

this.lng = lng;

}

void someMethod() {

}

}

var loc1 = new MyLocation();

var loc2 = new

MyLocation.withPosition(

57.123,37.22);

Getters and Setters

class MyLocation {

double _lat;

double _lng;

double get lat => _lat;

set lat (double value) {

if (value > 90 || value < -90) {

throw(“Invalid latitude”);

}

_lat = value;

}

double get lng => _lng;

set lng (double value) {

if (value > 180 ||

value < -180) {

throw(“Invalid longitude”);

}

_lng = value;

}

final arrived = false;

MyLocation() {

this.lat = 0;

this.lng = 0;

}

MyLocation.withPosition(

var lat, var lng) {

this.lat = lat;

this.lng = lng;

}

void someMethod() {

}

}

var loc1 = new MyLocation();

var loc2 = new

MyLocation.withPosition(

57.123,37.22);

loc1.lat = 57.123;

loc1.lng = 37.22;

loc2.lat = 999;

Inheritance

abstract class Shape {

double length;

double width;

Shape() {

this.length = 0;

this.width = 0;

}

Shape.withDimension(double length,

double width){

this.length = length;

this.width = width;

}

double perimeter() {

return 2 * (this.length +

this.width);

}

double area() {

return this.length * this.width;

}

}

class Rectangle extends Shape {

Rectangle() {}

Rectangle.withDimension(

double length, double width):

super.withDimension(

length, width);

}

Final Class

class Square extends Rectangle {

Square(double length):

super.withDimension(

length, length);

}

Square s = new Square(5);

print(s.area());

print(s.perimeter());

Overriding

class Circle extends Shape {

Circle(double radius):

super.withDimension(

radius, radius);

double area() {

return 3.14 * this.length *

this.length;

}

double perimeter() {

return (2 * 3.14 * this.length);

}

}

Circle c = new Circle(6);

print(c.area());

print(c.perimeter());

Interfaces

class CarInterface {

void accelerate() {

}

void decelerate() {}

void accelerateBy(int amount) {}

}

class MyCar implements

CarInterface {

void accelerate() {

}

void decelerate() {

}

void accelerateBy(int amount) {

}

}