2-1 Functions
- Function scope
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void main(List<String> arguments) { String nestedFunction() { return 'nested level'; } print(topFunction()); //top level scope print(nestedFunction()); //only valid inside of the outer function otherFunction(); //top level scope } String topFunction() { return 'top level'; } void otherFunction() { print(topFunction()); //nestedFunction(); <--invalid scope }
- output
top level nested level top level
- Function parameters
- arguments: passed in a function
- parameter: used within a function
- positional prameters: can't change the order
1 2 3
void function(int x, double y, String z) { //arguments passed in function(1, 1.5, 'called');//use parameters }
- optional positional parameters
1 2 3 4 5
//z is optional, should be nullable or defined a default value void function(int x, double y, [String z = 'default']) { function(1, 1.5); function(1, 1.5, 'optional'); }
- named parameters: the order doesn't matter. All optional by default.
1 2 3 4 5 6 7
void function({ int? x, double? y, String? z }) { function(y: 1.5); //put arguments by designating }
- required named parameters
1 2 3 4 5 6 7
void function({ required int x, required double y, required String z }) { function(x: 1, y: 1.5, z: 'required'); }
- mixed types parameters: positional parameters always come first
1 2 3 4 5 6 7
void function(int positional, { int?x, required double y, required String z }) { function(1, y: 1.5, z: 'required'); }
- Functional programming
- High order functions: take functions as parameters or return functions
1 2 3 4 5 6 7 8 9 10
void main(List<String> arguments) { //(x) {return x + 5;} is an anonymous function //twicePlusFive is also a function final twicePlusFive = twice((x) {return x + 5;}); print(twicePlusFive(3)); //output is 13. } //(return type) Function(param type) int Function(int) twice(int Function(int) f) { return (int x) {return f(f(x));}; }
- convert blocks to expressions:
cmd+.
1 2 3 4 5 6 7 8
void main(List<String> arguments) { final twicePlusFive = twice((x) => x + 5); print(twicePlusFive(3)); //output is 13. } int Function(int) twice(int Function(int) f) { return (int x) => f(f(x)); }
2-2 Collections
- List
- define
1 2 3 4
List<int> myList = [1, 2, 3]; //dart has no arrays but only lists final inferList = [1, 2, 3]; //will be inferred as List<int> final inferList2 = [1, 2, 'text']; //will be inferred as List<Object> <int>[1, 2, 3]; //no variable assignment
- access
1 2
int length = myList.length; final firstElement = myList[0];
- Map
- define
1 2 3 4 5
Map<String, dynamic> myMap = { 'name': 'riku', 'age': 0, 'is Chinese': true };
- access
1
final name = myMap['name'];
- Set
- define
1 2 3
Set<int> mySet = {1, 2, 3}; //a set can't contains duplicated values Set<int> dupSet = {1, 2, 3, 3}; print(dupSet.length); //output is 3
2-3 Transformations
- map
1 2 3 4
final names = ['Riku', 'CC', 'Cherry']; final nameLengths = names.map((e) => e.length).toList(); //will return a 'Literable<int>' if it's without 'toList' print(nameLengths[1]);//2
- where, forEach
1 2 3 4 5 6 7
final names = ['Riku', 'CC', 'Ch']; final nameFiltered = names.where((e) => e.length == 2).toList(); //direct print print(nameFiltered);//[CC, Ch] //Loops nameFiltered.forEach((element) => print(element)); nameFiltered.forEach(print);
- If, For, Spread
- If-Else inside of a list
1 2 3 4 5
bool isSignedIn = true; final dynamicList = <String>[ 'This is a fake content.', if (isSignedIn) 'Sign In' else 'Sign out' ];
- For inside of a list
1 2 3 4
final dynamicList = <String>[ for (int i = 0; i < 5; i++) i.toString(), for (final number in [6, 7, 8]) number.toString() ];
- Spread inside of a list
1 2 3 4 5 6 7 8
final list1 = <String>['e1', 'e2']; final list2 = <String>['e3', 'e4']; final concatList = <String>[ ...list1, 'em', ...list2, ]; print(concatList); //[e1,e2,em,e3,e4]
2-4 Enums
- enums
1 2 3 4 5 6 7 8
enum AccountType { free, premium, vip } void main(List<String> arguments) { final userAccountType = AccountType.free; print(userAccountType.index);//0 print(AccountType.values);//[AccountType.free, AccountType.premium, AccountType.vip] print(AccountType);//AccountType }
2-5 The Pub Package System
- dependencies/dev_dependencies
1 2 3 4 5
dependencies: #Is packaged into your app when it's built for publishing path: ^1.7.0 dev_dependencies: #Is not a part of your code pedantic: ^1.9.0
- add/update dependencies(VS code)
- hit
F1
-> type Pubspec Assist -> Add/update... - search from website: https://pub.dev/
- manually sync dependencies:
flutter pub get
for native flutter projects /dart pub get
for dart projects
- hit