You are on page 1of 2

Dart Cheat Sheet

Variables Optional Named Lambda Functions


var nums = new
int n1 = 5; // explicitly typed Parameters List<int>.generate(10, (i) => i);
var n2 = 4; // type inferred int addNums4(num1, {num2=0, num3=0}) print(nums);
// n2 = "abc"; // error { // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
dynamic n3 = 4; // var is same as return num1+num2+num3;
// dynamic } var odds = nums.where(
double n4; // n4 is null print(addNums4(1)); (n) => n % 2 == 1).toList();
String s1 = 'Hello, world!'; print(addNums4(1,num3:2)); print(odds); // [1, 3, 5, 7, 9]
var s2 = "Hello, world!"; print(addNums4(1,num2:5,num3:2));
var sum = nums.reduce(
Constants Parsing (s,n) => s + n);
print(sum); // 45
const PI = 3.14; // const is used var s1 = "123";
// for compile-time constant var s2 = "12.56"; var prices = nums.map(
var s3 = "12.a56"; (n) => "\$$n").toList();
final area = PI * 5*5; var s4 = "12.0"; print(prices);
// final variables can only be set print(num.parse(s1)); // 123 // [$0, $1, $2, $3, $4, $5, $6, $7,
// once print(num.parse(s2)); // 12.56 // $8, $9]
print(num.parse(s3));
// FormatException: 12.a56
Conditional Expressions Higher Order Functions
var grade = 3; print(num.tryParse(s3)); // null var names = ["Jimmy","TIM","Kim"];
var reply = grade > 3 ? "Cool":"Not // sort alphabetically with case
cool";
String Interpolation // insensitivity
names.sort(
var input; // input is null var s1 = "Hello"; (a, b) =>
var age = input ?? 0; var s2 = "world"; a.toUpperCase().compareTo(
print(age); // 0 var s3 = s1 + ", " + s2; b.toUpperCase())
var s = "${s3}!"; );
print(s); // Hello, world!
Functions print("Sum of 5 and 6 is ${5+6}");
print(names);
// [Jimmy, Kim, TIM]
void doSomething() { // Sum of 5 and 6 is 11
print("doSomething()"); // sort by length of name
}
int addNums1(num1, num2, num3) { List (Arrays) names.sort((a,b) {
if (a.length > b.length)
return num1+num2+num3; // dynamic list
var arr = [1,2,3,4,5]; return 1;
} else
print(arr.length); // 5
print(arr[1]); // 2 return -1;
doSomething(); });
print(addNums1(1,2,3)); arr[4] *= 2;
print(arr[4]); // 10 print(names);
arr.add(6); // [Kim, TIM, Jimmy]
Arrow Syntax print(arr); // [1, 2, 3, 4, 10, 6]
void doSomethingElse() {
doSomething(); List arr2; List bubbleSort(List items, bool
} arr2 = arr; Function (int,int) compareFunction)
arr2[1] = 9; {
// the above can be rewritten using for (var j=0; j<items.length-1;
// arrow syntax print(arr); // [1, 9, 3, 4, 10, 6] j++) {
void doSomethingElse() => print(arr2); // [1, 9, 3, 4, 10, 6] var swapped = false;
doSomething(); for (var i=0;
// fixed size list i<items.length-1-j; i++) {
var arr3 = new List(3); if (!compareFunction(items[i],
Optional Positional print(arr3); // [null, null, null] items[i+1])) {
var t = items[i+1];
Parameters arr3.add(5);
// Uncaught exception: items[i+1] = items[i];
int addNums2(num1, [num2=0, num3=0]) // Unsupported operation: add items[i] = t;
{ swapped = true;
return num1+num2+num3; }
} Map }
var details = {"name":"Sam", if (!swapped) break;
print(addNums2(1)); "age":"40"}; }
print(addNums2(1,2)); print(details); return items;
print(addNums2(1,2,3)); }
var devices = new Map();
Named Parameters var apple = ["iPhone","iPad"];
var samsung = ["S10","Note 10"];
var nums = [5,2,8,7,9,4,3,1];
// named parameters devices["Apple"] = apple; // sort in ascending order
int addNums3({num1, num2, num3}) { devices["Samsung"] = samsung; var sortedNums = bubbleSort(nums,
return num1+num2+num3; for (String company in (n1,n2) => n1<n2);
} devices.keys) { print(sortedNums);
print(company);
print(addNums3( for (String device in // sort in descending order
num1:1,num2:2,num3:3)); devices[company]) { sortedNums = bubbleSort(nums,
print(device); (n1,n2) => n1>n2);
} print(sortedNums);
}
1


Rev 1.1 © Wei-Meng Lee , Developer Learning Solutions, http://calendar.learn2develop.net All rights reserved.
} }
Iterations } double area() {
for (int i=0;i<5; i++) { return this.length * this.width;
print(i); var loc1 = new MyLocation(); }
} // prints 0 to 4 var loc2 = new }
MyLocation.withPosition(
var list = [1,2,3,4,5]; 57.123,37.22); class Rectangle extends Shape {
for (final i in list) { Rectangle() {}
print(i); Rectangle.withDimension(
} // prints 1 to 5 Getters and Setters double length, double width):
class MyLocation { super.withDimension(
int i=0; double _lat; length, width);
while (i < 5) { double _lng; }
print(i); double get lat => _lat;
i++; set lat (double value) {
} // prints 0 to 4 if (value > 90 || value < -90) { Final Class
throw("Invalid latitude"); // Square cannot be extended (it
i = 0; } // does not have a zero-argument
do { _lat = value; // constructor)
print(i); } class Square extends Rectangle {
i++; Square(double length):
} while (i<5); double get lng => _lng; super.withDimension(
// prints 0 to 4 set lng (double value) { length, length);
if (value > 180 || }
value < -180) {
Class throw("Invalid longitude"); Square s = new Square(5);
class MyLocation { } print(s.area()); // 25
} _lng = value; print(s.perimeter()); // 20
}
// type inference
var loc1 = new MyLocation(); // read-only property Overriding
final arrived = false; class Circle extends Shape {
// declare and initialize Circle(double radius):
MyLocation loc2 = new MyLocation(); // unnamed constructor super.withDimension(
MyLocation() { radius, radius);
double area() {
Properties this.lat = 0;
this.lng = 0; return 3.14 * this.length *
class MyLocation { } this.length;
// read/write properties }
var lat; // named constructor double perimeter() {
var lng; MyLocation.withPosition( return (2 * 3.14 * this.length);
var lat, var lng) { }
// read-only property this.lat = lat; // overloading of methods not
final arrived = false; this.lng = lng; // supported in Dart
} } }

loc1.lat = 57.123; void someMethod() { Circle c = new Circle(6);


loc1.lng = 37.22; } print(c.area());
// loc1.arrived = true; // error } // 113.03999999999999
var arr = loc1.arrived;
var loc1 = new MyLocation(); print(c.perimeter()); // 37.68
Methods var loc2 = new
class MyLocation { MyLocation.withPosition(
57.123,37.22);
Static Members/Methods
// read/write properties class Car {
var lat; static var MilesToKM = 1.60934;
var lng; loc1.lat = 57.123;
loc1.lng = 37.22; static double kilometersToMiles(
// read-only property double km) {
final arrived = false; return km / 1.60934;
void someMethod() { loc2.lat = 999;
// Uncaught exception:Invalid }
} void accelerate() {}
} // latitude
void decelerate() {}
void stop() {}
loc1.someMethod(); Inheritance void printSpeed() {}
// abstract class cannot be }
Constructors // instantiated directly
class MyLocation { abstract class Shape {
double length;
Interfaces
// read/write properties class CarInterface {
var lat; double width;
void accelerate() {
var lng; // default implementation
// without this zero-argument
// constructor, class cannot be ...
// read-only property }
final arrived = false; // extended
Shape() { void decelerate() {}
this.length = 0; void accelerateBy(int amount) {}
// unnamed constructor }
MyLocation() { this.width = 0;
this.lat = 0; }
class MyCar implements
this.lng = 0; CarInterface {
} // constructor with another name
Shape.withDimension(double length, void accelerate() {
double width){ }
// named constructor void decelerate() {
MyLocation.withPosition( this.length = length;
this.width = width; }
var lat, var lng) { void accelerateBy(int amount) {
this.lat = lat; }
double perimeter() { }
this.lng = lng; }
} return 2 * (this.length +
void someMethod() { this.width);


Rev 1.1 © Wei-Meng Lee , Developer Learning Solutions, http://calendar.learn2develop.net All rights reserved.

You might also like