[go: up one dir, main page]

100% found this document useful (5 votes)
4K views2 pages

Dart Cheat Sheet

The document provides an overview of Dart programming concepts including variables, constants, functions, conditionals, lists, maps, and more. Key concepts covered include variable typing, optional and named parameters for functions, string interpolation, parsing strings to numbers, higher order functions to sort and filter lists, and bubble sort algorithm.

Uploaded by

Muzamil Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
4K views2 pages

Dart Cheat Sheet

The document provides an overview of Dart programming concepts including variables, constants, functions, conditionals, lists, maps, and more. Key concepts covered include variable typing, optional and named parameters for functions, string interpolation, parsing strings to numbers, higher order functions to sort and filter lists, and bubble sort algorithm.

Uploaded by

Muzamil Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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; // dynamic means n3 return num1+num2+num3;
// can take on any } var odds = nums.where(
// type print(addNums4(1)); (n) => n % 2 == 1).toList();
n3 = "abc"; print(addNums4(1,num3:2)); print(odds); // [1, 3, 5, 7, 9]
double n4; // n4 is null print(addNums4(1,num2:5,num3:2));
String s1 = 'Hello, world!'; var sum = nums.reduce(
var s2 = "Hello, world!";
Parsing (s,n) => s + n);
print(sum); // 45
var s1 = "123";
Constants var s2 = "12.56"; var prices = nums.map(
const PI = 3.14; // const is used var s3 = "12.a56"; (n) => "\$$n").toList();
// for compile-time constant var s4 = "12.0"; print(prices);
print(num.parse(s1)); // 123 // [$0, $1, $2, $3, $4, $5, $6, $7,
final area = PI * 5*5; print(num.parse(s2)); // 12.56 // $8, $9]
// final variables can only be set print(num.parse(s3));
// once // FormatException: 12.a56
Higher Order Functions
print(num.tryParse(s3)); // null var names = ["Jimmy","TIM","Kim"];
Conditional Expressions // sort alphabetically with case
var grade = 3;
var reply = grade > 3 ? "Cool":"Not String Interpolation // insensitivity
names.sort(
cool"; var s1 = "Hello"; (a, b) =>
var s2 = "world"; a.toUpperCase().compareTo(
var input; // input is null var s3 = s1 + ", " + s2; b.toUpperCase())
var age = input ?? 0; var s = "${s3}!"; );
print(age); // 0 print(s); // Hello, world! print(names);
print("Sum of 5 and 6 is ${5+6}"); // [Jimmy, Kim, TIM]
// Sum of 5 and 6 is 11
Functions // sort by length of name
void doSomething() {
print("doSomething()"); List (Arrays) names.sort((a,b) {
if (a.length > b.length)
} // dynamic list
var arr = [1,2,3,4,5]; return 1;
int addNums1(num1, num2, num3) { else
return num1+num2+num3; print(arr.length); // 5
print(arr[1]); // 2 return -1;
} });
arr[4] *= 2;
print(arr[4]); // 10 print(names);
doSomething(); // [Kim, TIM, Jimmy]
print(addNums1(1,2,3)); arr.add(6);
print(arr); // [1, 2, 3, 4, 10, 6]

Arrow Syntax List arr2; List bubbleSort(List items, bool


void doSomethingElse() { arr2 = arr; Function (int,int) compareFunction)
doSomething(); arr2[1] = 9; {
} for (var j=0; j<items.length-1;
print(arr); // [1, 9, 3, 4, 10, 6] j++) {
// the above can be rewritten using print(arr2); // [1, 9, 3, 4, 10, 6] var swapped = false;
// arrow syntax for (var i=0;
void doSomethingElse() => // fixed size list i<items.length-1-j; i++) {
doSomething(); var arr3 = new List(3); if (!compareFunction(items[i],
print(arr3); // [null, null, null] items[i+1])) {
arr3.add(5); var t = items[i+1];
Optional Positional // Uncaught exception: items[i+1] = items[i];
items[i] = t;
Parameters // Unsupported operation: add
swapped = true;
int addNums2(num1, [num2=0, num3=0]) }
{ Map }
return num1+num2+num3; var details = {"name":"Sam", if (!swapped) break;
} "age":"40"}; }
print(details); return items;
print(addNums2(1)); }
print(addNums2(1,2)); var devices = new Map();
print(addNums2(1,2,3)); var apple = ["iPhone","iPad"]; var nums = [5,2,8,7,9,4,3,1];
var samsung = ["S10","Note 10"];
Named Parameters devices["Apple"] = apple;
devices["Samsung"] = samsung;
// sort in ascending order
var sortedNums = bubbleSort(nums,
// named parameters for (String company in (n1,n2) => n1<n2);
int addNums3({num1, num2, num3}) { devices.keys) { print(sortedNums);
return num1+num2+num3; print(company);
} for (String device in // sort in descending order
devices[company]) { sortedNums = bubbleSort(nums,
print(addNums3( print(device); (n1,n2) => n1>n2);
num1:1,num2:2,num3:3)); } print(sortedNums);
}
1


Rev 1.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.1 © Wei-Meng Lee , Developer Learning Solutions, http://calendar.learn2develop.net All rights reserved.

You might also like