assert(x.toRadixString(8) == ’12’);
assert(y.toStringAsFixed(2) == ‘1.50’);
var z = double.tryParse(‘3.14’);
assert(z == 3.14);
var name = ‘Alex’;
assert(‘The length of $name is ${name.length}’ == ‘The length of Alex is 4’);
var list1 = [1, 2, 3];
var list2 = List<int>(3);
var map1 = {‘a’: ‘A’, ‘b’: ‘B’};
var map2 = Map<String, String>();
Runes
var value = ‘\u{1F686} \u{1F6B4}’;
assert(Symbol(‘a’) == #a);
enum TrafficColor { red, green, yellow }
assert(TrafficColor.red.index == 0);
assert(TrafficColor.values.length == 3);
Both Object and dynamic permit all values.
dynamic value = 1;
Functions in Dart are objects and have the type Function
int sum(List<int> list, [int initial = 0]) {
var total = initial;
list.forEach((v) => total += v);
return total;
}
String joinToString(List<String> list,
{@required String separator, String prefix = “, String suffix = “}) =>
‘$prefix${list.join(separator)}$suffix’;
void main() {
assert(sum([1, 2, 3]) == 6);
assert(sum([1, 2, 3], 10) == 16);
assert(joinToString([‘a’, ‘b’, ‘c’], separator: ‘,’) == ‘a,b,c’);
assert(joinToString([‘a’, ‘b’, ‘c’], separator: ‘-‘, prefix: ‘*’, suffix: ‘?’) ==’*a-b-c?’);
}
var list = [1, 2, 3]; list.forEach((v) => print(v * 10));
typedef Processor<T> = void Function(T value);
void process<T>(List<T> list, Processor<T> processor) {
list.forEach((item) {
print(‘processing $item’);
processor(item);
print(‘processed $item’);
});
}