Skip to content

Instantly share code, notes, and snippets.

@michaelvdnest
Last active February 6, 2020 08:15
Show Gist options
  • Save michaelvdnest/b0978968f04eff2336f8ddcc6d6deba3 to your computer and use it in GitHub Desktop.
Save michaelvdnest/b0978968f04eff2336f8ddcc6d6deba3 to your computer and use it in GitHub Desktop.
A Dart Cheatsheet

Comments

// This is a single line comment

/*
  This is a multiline comment
*/

/// This is a single line documentation comment

/**
  This is a multiline
  documentation comment.
*/

Variables

var x;                    // x has a value of null
var x = "A string";       // Infer string type from value
String x = "A string";    // Declare type explicitly
dynamic x = "John Doe";   // Can have value of any type
x = 36;
Object x = Person();      // Preferably use to indicate an object
// Compile time constant
const s = "John Doe";     
List lst = const [ 1, 2, 3];
lst[0] = 0;               // Error, list is const.
// Immutable value
final d = DateTime.now(); // Set at once at runtime 
d = DateTime.now();       // Error, d is immutable

Strings

// Strings can be initialized using single or double quotes
String name1 = 'John';
String name2 = "Doe";
// Strings can include expressions
String s = 'My name is $name1.';           
String s = 'My name is ${name1}.';         //Avoid {} for simple identifier
// Composing strings
String s = "My name is " "John " "Doe"                        // Prefered
String s = "My name is "+ "John " + "Doe."                    // Avoid
String s = 'Hello, $name! You are ${year - birth} years old.'

Numbers

num i = 5;    // int inferred
num d = 5.5   // double inferred
// turn into string
String si = i.toString();
String sd = d.toString(); 
// turn into number
int i = int.parse("5");
double d = double.parse("5.5);

Lists

List list1 = [ 1, 2, 3 ];     // [1, 2, 3]
Object list2 = [ 1, 2, 3 ];   // [1, 2, 3]
var list3 = [0, ...lst1];     // [0, 1, 2, 3]
var fixed = List(2);          // [null, null]
v
// Null aware spread operator ...?
list4 = null
lst = ['a', ...?list4, 'b']         // ['a', 'b']
// Methods and properties
lst.add('c');                       // [a, b, c] 
lst.addAll(['e', 'd']);             // [a, b, c, e, d]
lst.sort((a, b) => a.compareTo(b)); // [a, b, c, d, e]
lst.length;                         // 5
lst.indexOf('b');                   // 1
lst.removeLast();                   // [a, b, c, d]  
lst.clear();                        // []
// collection if
var lst = [1, 2, if (b) 3, 4];  // [1, 2, 3, 4]
// collection for
var ints = [1, 2];
var strs = ['#0', for (var i in ints) '#$i']; // [#0, #1, #2]

Sets

// An unordered collection of unique items
Set metals = Set();
metals.addAll(["gold", "platinum", "titanium"]);
metals.add("gold");         // No harm, no foul
metals.remove("platinum");  // ["gold", "titanium"]
metals.contains("gold"));   // true
metals.containsAll(["gold", "titanium"]));
// Intersection and union
var more = Set.from(['gold', 'silver']);
var i = metals.intersection(more] // ["gold"];
var u = metals.union(more) // ["gold", "platinum", "silver"]

Maps

// An unordered collection of key-value pairs

// Maps often use strings as keys. var elements = { 'metals': ['gold', 'platinum'], 'rare': ['uranium', 'plutonium'], 'alkali': ['lithium', 'sodium'] };

// Maps can be built from a constructor. var searchTerms = Map();

// define the types of the key and value var alkali = Map<int, String>(); alkali[3] = 'lithium'; alkali[11] = 'sodium';

var actors = { "Ryan Reynolds" : "Deadpool", "Hugh Jackman" : "Wolverine" };

var actresses = Map(); actresses["scarlett johansson"] = "Black Widow"; actresses["Zoe Saldana"] = "Gamora";

var movies = Map<String, int>(); movies["Iron Man"] = 3; movies["Thor"] = 3;



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment