-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dart Tutorial.dart
284 lines (222 loc) · 6.19 KB
/
Dart Tutorial.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Dart cheat sheet tutorial code lab
// https://dart.dev/codelabs/dart-cheatsheet
// String interpolation
String stringify(int x, int y) {
// Return a formatted string here
return '$x $y';
}
// =======================
// Null-aware operators
String foo = 'a string';
String bar; // Unassigned objects are null by default.
// Substitute an operator that makes 'a string' be assigned to baz.
String baz = foo ?? bar;
void updateSomeVars() {
// Substitute an operator that makes 'a string' be assigned to bar.
bar ??= 'a string';
}
// =======================
// Conditional property access
// This method should return the uppercase version of `str`
// or null if `str` is null.
String upperCaseIt(String str) {
// Try conditionally accessing the `toUpperCase` method here.
return str?.toUpperCase();
}
// =======================
// Collection literals
// Assign this a list containing 'a', 'b', and 'c' in that order:
final aListOfStrings = ['a', 'b', 'c'];
// Assign this a set containing 3, 4, and 5:
final aSetOfInts = {3,4,5};
// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12:
final aMapOfStringsToInts = {'myKey': 12};
// Assign this an empty List<double>:
final anEmptyListOfDouble = <double>[];
// Assign this an empty Set<String>:
final anEmptySetOfString = <String>{};
// Assign this an empty Map of double to int:
final anEmptyMapOfDoublesToInts = <double, int>{};
// =======================
// Arrow syntax
class MyClass {
int _value1 = 2;
int _value2 = 3;
int _value3 = 5;
// Returns the product of the above values:
int get product => _value1 * _value2 * _value3;
// Adds 1 to _value1:
void incrementValue1() => _value1++;
// Returns a string containing each item in the
// list, separated by commas (e.g. 'a,b,c'):
String joinWithCommas(List<String> strings) => strings.join(',');
}
// =======================
// Cascades
class BigObject {
int anInt = 0;
String aString = '';
List<double> aList = [];
bool _done = false;
void allDone() {
_done = true;
}
}
BigObject fillBigObject(BigObject obj) {
// Create a single statement that will update and return obj:
return obj..anInt = 1
..aString = 'String!'
..aList.add(3)
..allDone();
}
// =======================
// Getters and setters
class InvalidPriceException {}
class ShoppingCart {
List<double> _prices = [];
// Add a "total" getter here:
double get total => _prices.fold(0, (e,t) => e+t);
// Add a "prices" setter here:
set prices(List<double> value) {
if (value.any((p) => p < 0)) {
throw InvalidPriceException();
}
_prices = value;
}
}
// =======================
// Optional positional parameters
String joinWithCommas(int a, [int b, int c, int d, int e]) {
var total = '$a';
if (b != null) total = '$total,$b';
if (c != null) total = '$total,$c';
if (d != null) total = '$total,$d';
if (e != null) total = '$total,$e';
return total;
}
// =======================
// Optional named parameters
class MyDataObject {
final int anInt;
final String aString;
final double aDouble;
MyDataObject({
this.anInt = 1,
this.aString = 'Old!',
this.aDouble = 2.0,
});
// Add your copyWith method here:
MyDataObject copyWith({int newInt, String newString, double newDouble}) {
return MyDataObject(
anInt : newInt ?? this.anInt,
aString : newString ?? this.aString,
aDouble: newDouble ?? this.aDouble,
);
}
}
// =======================
// Exceptions
typedef VoidFunction = void Function();
class ExceptionWithMessage {
final String message;
const ExceptionWithMessage(this.message);
}
// Call logException to log an exception, and doneLogging when finished.
abstract class Logger {
void logException(Type t, [String msg]);
void doneLogging();
}
void tryFunction(VoidFunction untrustworthy, Logger logger) {
// Invoking this method might cause an exception. Catch and handle
// them using try-on-catch-finally.
try {
untrustworthy();
} on ExceptionWithMessage catch (e) {
logger.logException(e.runtimeType, e.message);
} on Exception {
logger.logException(Exception);
} catch(e) {
rethrow;
} finally {
logger.doneLogging();
}
}
// =======================
// Using this in a constructor
class MyClass {
final int anInt;
final String aString;
final double aDouble;
// Create a constructor here.
MyClass(this.anInt, this.aString, this.aDouble);
}
// =======================
// Initializer lists
class FirstTwoLetters {
final String letterOne;
final String letterTwo;
// Create a constructor with an initializer list here:
FirstTwoLetters(String word)
: letterOne = word[0],
letterTwo = word[1];
}
// =======================
// Named constructors
class Color {
int red;
int green;
int blue;
Color(this.red, this.green, this.blue);
// Create a named constructor called "black" here:
Color.black() {
red = 0;
green = 0;
blue = 0;
}
}
// =======================
// Factory constructors
class IntegerHolder {
IntegerHolder();
// Create your factory constructor called "fromList" here.
factory IntegerHolder.fromList(List<int> list) {
if (list.length == 1) return IntegerSingle(list[0]);
if (list.length == 2) return IntegerDouble(list[0], list[1]);
if (list.length == 3) return IntegerTriple(list[0], list[1], list[2]);
return null;
}
}
class IntegerSingle extends IntegerHolder {
final int a;
IntegerSingle(this.a);
}
class IntegerDouble extends IntegerHolder {
final int a;
final int b;
IntegerDouble(this.a, this.b);
}
class IntegerTriple extends IntegerHolder {
final int a;
final int b;
final int c;
IntegerTriple(this.a, this.b, this.c);
}
// =======================
// Redirecting constructors
class Color {
int red;
int green;
int blue;
Color(this.red, this.green, this.blue);
// Create a named constructor called "black" here and redirect it
// to call the existing constructor
Color.black() : this(0, 0, 0);
}
// =======================
// Const constructors
class Recipe {
final List<String> ingredients;
final int calories;
final double milligramsOfSodium;
const Recipe(this.ingredients, this.calories, this.milligramsOfSodium);
}