75% found this document useful (4 votes)
6K views

Dart For Flutter Cheat Sheet PDF

This document provides a cheat sheet overview of key Dart programming concepts for Flutter including built-in types, control flow statements, variables, functions, classes, mixins, abstract classes, asynchronous programming, and more. It covers topics such as lists, maps, optional and named parameters, anonymous functions, factories, and futures.

Uploaded by

Fendy Kwan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
75% found this document useful (4 votes)
6K views

Dart For Flutter Cheat Sheet PDF

This document provides a cheat sheet overview of key Dart programming concepts for Flutter including built-in types, control flow statements, variables, functions, classes, mixins, abstract classes, asynchronous programming, and more. It covers topics such as lists, maps, optional and named parameters, anonymous functions, factories, and futures.

Uploaded by

Fendy Kwan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Dart for Flutter Cheat Sheet 1

Build-in types Control flow statements


Numbers: num, int, double // if else
Strings: String, StringBuffer if (isRaining()) {
you.bringRainCoat();
Booleans: bool, true, false
} else if (isSnowing()) {
Lists (arrays): [0,1,1,2] you.wearJacket();
Sets (unique): {'A', 'B', 'C'} } else {
Maps: {'key': 'value'} car.putTopDown();
}
// for loops
Variables
for (var i = 0; i < 5; i++) {
var name = 'Bob'; print(i);
dynamic name = 'Bob'; }
String name = 'Bob' + 'Marley'; // while
List<String> myList = ['B','O','B']; while (!isDone()) {
doSomething();
var mySet = <String> {}; }
var myMap = {54: 'xenon'}; do {
final name = 'Bob'; // set only once printLine();
} while (!atEndOfPage());
const bar = 1000000; // compile-time
// switch case
var command = 'OPEN';
Functions
switch (command) {
int addNumber (int num1, int num2) { case 'CLOSED':
return num1 + num2; executeClosed();
}
break;
// omit the types
case 'OPEN':
addNumber (num1, num2) { executeOpen();
return num1 + num2; break;
}
default:
// named parameters
executeUnknown();
void enableFlags ({bool bold, bool }
hidden}) {...}
// assert (development only)
enableFlags (bold: true, hidden:
false);
assert (number < 100);
// required
Exceptions
Scrollbar ({Key key, @required Widget
child}) try {
// default parameter values breedMoreLlamas();
enableFlags ({bool bold = false, bool } catch (e) {
hidden = false}) {...}
print('Error: $e');
// anonymous functions
} finally {
var list = ['apples','bananas'];
cleanLlamaStalls();
list.forEach ( (item) =>
print('${list.indexOf(item)}: $item')); }
});

cc-by Mobile App Development with Google’s Flutter and Firebase: from Zero to Hero https://github.com/FlutterInThai
Dart for Flutter Cheat Sheet 2
Classes Abstract classes
class Point { abstract class Doer {
num x, y;
// static variable
void doSomething();
static const fixedNumber = 16; }
// constructor class EffectiveDoer extends Doer {
Point(this.x, this.y); void doSomething() {
// named constructor print('something');
Point.origin() {
x = 0;
}
y = 0; }
} class Greeter implements
// initializer constructor EffectiveDoer {
Point.fromJson(Map<String, num> json) doSomething () {
: x = json['x'], print('Hello');
y = json['y'] { }
print('In Point.fromJson(): ($x, $y)'); }
}
}
// invoking non-default constructor Mixins
class Employee extends Person { // multiple class hierarchies
Employee.fromJson(Map data) : class Musician extends Performer with
super.fromJson(data) { Musical, Conductor, Composer {
// do something }
} mixin Musical {
} bool canPlayPiano = true;
// factory constructors void entertainMe() {
class Logger {
print('Playing piano');
final String name;
bool mute = false; }
}
static final Map<String, Logger> _cache =
<String, Logger>{};
Asynchrony
factory Logger(String name) {
if (_cache.containsKey(name)) {
Future checkVersion() async {
return _cache[name]; try {
} else {
version = await lookUpVersion();
final logger =
Logger._internal(name); } catch (e) {
_cache[name] = logger; Print(e.toString);
return logger;
} }
} // Do something with version
Logger._internal(this.name); }

void log(String msg) {


if (!mute) print(name + ' ' + msg);
}
}

cc-by Mobile App Development with Google’s Flutter and Firebase: from Zero to Hero https://github.com/FlutterInThai

You might also like