forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dart Cheet Sheet.txt
448 lines (301 loc) · 12.7 KB
/
Dart Cheet Sheet.txt
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# dart-cheat-sheet
A curated list of awesome stuff needed to get started with your flutter development journey
## String interpolation
Every language has it's own way of interpolating two or more words or characters. In dart, you can put value of an expression inside a string as follows:
### Example
```dart
int x=6;
int y=2;
String sum = '${x+y}'; // result is 8
String subtraction = '${x-y}'; // result is 4
String upperCase = '${"hello".toUpperCase()}'; // result is HELLO
String concatXY = '$x$y'; // result is '62'
```
## Functions
Dart lang is an OOL(Object-oriented language), In this language, functions are objects and have a type, Function. This implies that functions can be assigned to
variables or passed as args to other functions. Interestingly, you can also call an instance of a class as if it were a fuction. That's awesome, right?
### Example
```dart
String fullName(){
String firstName = "Temidayo";
String lastName = "Adefioye";
return '$firstName $lastName'; // returns 'Temidayo Adefioye'
}
```
```dart
int length(String text){
return text.length; // returns length of text
}
```
The above function can be rewritten in a more concise way:
```dart
int length(String text) => return text.length; // returns length of text
```
The above approach is applicable where functions contain just ONE expression. This is also referred to as shorthand syntax.
## Parsing
Parsing a string to a number such as integer and double is very key. As a developer, often times I need to convert(parse) a string value
coming from a server-side to a number, tryParse method comes in handy. Take a look at this code snippet:
### Example
```dart
var a = "121";
var b = "120.56";
var c = "100.a12";
var d = "abc";
String parseA = int.tryParse(a); // result is 121
String parseB = double.tryParse(b); // result is 120.56
String parseC = double.tryParse(c); // result is null (that string contains invalid number)
String parseD = double.tryParse(d); // result is null (that string contains invalid number)
```
## List Arrays
Perhaps the most common collection in nearly every programming language is the array or ordered set of objects. Please note that Dart arrays are Lists.
### Example
```dart
var numList = [1,2,3,5,6,7];
var countryList = ["Nigeria","United States","United Kingdom","Ghana","IreLand","Germany"];
String numLength = numList.length; // result is 6
String countryLength = countryList.length; // result is 6
String countryIndex = countryList[1]; // result is 'United States'
String numIndex = numList[0]; // result is 1
countryList.add("Poland"); // Adds a new item to the list.
var emailList = new List(3); // Set a fixed list size
var emailList = new List<String>(); // instance of a list of type String
```
## Lambda Functions
Lambda functions provide you with a short and concise way of representing small functions. They are also referred to as Arrow functions. In dart, if you need to write quick throw away functions without necessarily naming them, lambda fucntion is all you need. With the power of this function you can do the following and more:
### Example
```dart
var numList = new List<int>.generate(5,(i) => i);
print(numList); //result: {0,1,2,3,4}
var loans = numList.map( (n) => "\#$n").toList();
print(loans); // result: {#0, #1, #3, #4}
printMsg()=> print("Hello world");
// You can declare a state function this way in flutter
_DashboardState createState() => _DashboardState();
// How about creating a widget using lambda?
Card makeCard(Asset assetViewModel) => Card(
elevation: 8.0,
margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
child: Container(
decoration: BoxDecoration(color: Colors.white),
child: makeListTile(assetViewModel), // where makeListTile is a custom widget created already
),
);
```
## Null-aware Operators
Handling null exceptions in app development is very essential, as this allows you to create a seamless experience for your app users. Dart provides you with some handy operators for dealing with values that might be null. One is the ??= assignment operator, which assigns a value of a variable only if that variable is currently null:
### Example
```dart
int x; // The initial value of any object is null
x ??=6;
print(x); // result: 6
x ??=3;
print(x); // result is still 6
print(null ?? 10); // result: 10. Display the value on the left if it's not null else return the value on the right
```
## Conditional Property Access
To properly safegaurd access to a property or method of an object that might be null, put a question mark (?) before the (.)
### Example
```dart
userObject?.userName
//The code snippet above is equivalent to following:
(userObject !=null) ? userObject.userName : null
//You can chain multiple uses of ?. together in a single expression
userObject?.userName?.toString()
// The preceeding code returns null and never calls toString() if either userObject or userObject.userName is null
```
## Collections Literals
With literals you can create Dart's built-in lists, maps and sets without hassle.
### Example
```dart
final fruitList = ["Orange","Bannana","Carrot","Apple"]; // A list of fruit
final countrySet = {"Nigeria","United States","Poland","Italy"}; // A set of country
final credMap = {
'userName': 'temi',
'password': 'xmen'
} // A map of user credentials
```
You maybe wondering why we didn't explicitly declare a type for all of the collections above. It's interesting to know that dart's type inference can assign types to these variables for you. In this case, the inferred types are List<String>, Set<String> and Map<String,String>.
Please note that you can choose to specify a type for your variable declaration like this:
```dart
final fruitList = <String>[];
final countrySet = <String>{};
final credMap = <String, String>{};
```
## Arrow Syntax
Remember Lambda Functions as discussed earlier in this sheet? We used a symbol => to define our lambda expression. You can check out Lambda function section for more explanation.
### Example
```dart
String _firstName = "Michael";
String _lastName = "Jones";
String _middleName = "Will";
String get fullName => '$_firstName $_middleName $_lastName'; // result: 'Michael Will Jones'
```
## Iterations
Just like every other programming language out there, you can perform iterations in dart. Here is a for loop example
### Example
```dart
for (int i=0; i<=20; i++){
print(i); // prints 1 to 20
}
var fruitList = ["Orange","Bannana","Carrot","Apple"];
for (final fruit in fruits){
print(fruit); // prints all types of fruit in the list
}
```
## Map
Map can either be declared using literals or map constructor. To learn more about map declaration using literals, please go to the Collections Literals section. Here is how you can declare map using a map constructor:
### Example
```dart
var user = new Map();
// To initialize the map, do this:
user['firstName'] = 'Paul';
user['lastName'] = 'Pogba';
// Result: {firstName: Paul, lastName: Pogba}
// Below are map properties
- Keys
- Values
- Length
- isEmpty
- isNotEmpty
// Below are map functions
- addAll()
- clear()
- remove()
- forEach()
```
## Variables
Here's an example of creating a variable and initializing it:
### Example
```dart
int x = 2; // explicitly typed
var p = 5; // type inferred
p = "cool"; // ops! throws an error
dynamic z = 8; // variable can take on any type
z = "cool"; // cool
// if you never intend to change a variable use final or const. Something like this:
final email = "[email protected]"; // you can't change the value
final String email = "[email protected]"; // you can't change the value
// iPhone 11 Pro max calculator using const
const qty = 5;
const totalCost = 1099 * qty;
```
## Class
In Dart, the class keyword is used to declare a class. Here is a basic example:
### Example
```dart
class Car {
// field
String engine = "E1001";
// function
void disp() {
print(engine);
}
}
```
## Getters and Setters
Getters and setters are special methods that provide read and write access to an object’s properties. Each instance variable of your class has an implicit getter, and a setter if needed. In dart, you can take this even further by implementing your own getters and setters. If you've had any experience in Object-Oriented Programming you'll feel right at home. Here is a basic syntax for a class:
### Example
```dart
class className {
fields;
getters/setters
constructor
methods/functions
}
```
```dart
class Person {
String firstName;
String lastName;
double height;
int personAge;
int yearofBirth;
double weight;
int get age {
return personAge;
}
void set age(int currentYear) {
personAge = currentYear - yearofBirth;
}
// We can also eliminate the setter and just use a getter.
//int get age {
// return DateTime.now().year - yearofBirth;
//}
Person({this.firstName,this.lastName,this.personAge,this.yearofBirth,this.weight});
}
```
We can implement Person class this way:
```dart
void main() {
Person person = Person(firstName:"Thanos",lastName:"Rednos",yearofBirth:1990,weight:200.5);
print(person.firstName); // output - Thanos
print(person.lastName); // output - Rednos
person.age = 2019;
print(person.age); // output - 29
}
```
## Futures: Async and Await
The async and await keywords provide a declarative way to define asynchronous functions and use their results. Remember these two basic guidelines when using async and await:
- To define an async function, add async before the function body:
- The await keyword works only in async functions.
### Example
```dart
Future<String> login() {
// Imagine that this function is
// more complex and slow.
String userName="Temidjoy";
return
Future.delayed(
Duration(seconds: 4), () => userName);
}
// Asynchronous
main() async {
print('Authenticating please wait...');
print(await userName());
}
```
## JSON and serialization
Most mobile and web apps use JSON for tasks such as exchanging data with a web server. With Dart support for JSON serialization and deserialization: converting Dart objects to and from JSON, data exchange is made easy in flutter development.
The following libraries and packages are useful across Dart platform:
- [dart:convert](https://dart.dev/guides/libraries/library-tour#dartconvert---decoding-and-encoding-json-utf-8-and-more)
Converters for both JSON and UTF-8 (the character encoding that JSON requires).
- [package:json_serializable](https://pub.dev/packages/json_serializable)
An easy-to-use code generation package. When you add some metadata annotations and use the builder provided by this package, the Dart build system generates serialization and deserialization code for you.
- [package:built_value](https://pub.dev/packages/built_value)
A powerful, opinionated alternative to json_serializable.
You need to serialize and deserialize JSON in your Flutter project? [see this example](https://flutter.dev/docs/development/data-and-backend/json) to quickly get started.
## Reading and decoding a file
The code snippet below reads a file and runs two transforms over the stream. It first converts the data from UTF8 and then runs it through a LineSplitter. All lines are printed, except any that begin with a hashtag, #.
### Example
```dart
import 'dart:convert';
import 'dart:io';
Future<void> main(List<String> args) async {
var file = File(args[0]);
var lines = utf8.decoder
.bind(file.openRead())
.transform(LineSplitter());
await for (var line in lines) {
if (!line.startsWith('#')) print(line);
}
}
```
## Sound null safety
Sound null safety is now supported by the Dart language!
When you choose to use null safety, types in your code are by default non-nullable, which means variables cannot contain null unless you explicitly allow it. Your runtime null-dereference mistakes become edit-time analysis errors with null safety.
The variables in the following code are all non-nullable thanks to null safety:
```dart
// None of these can ever be null in null-safe Dart.
var x = 81; // Inferred to be an int.
String address = getAddress();
final c = Car();
```
Simply add ? to a variable's type declaration to indicate that it might be null:
```dart
// This is now a nullable Int.
int? x = null;
```
### Enabling null safety
Sound null safety is available in Dart 2.12 and Flutter 2.
### Migrating an existing package or app
For instructions on how to migrate your code to null safety, see the [migration guide](https://dart.dev/null-safety/migration-guide).