var hi ="hello"; // Can't change type.
dynamic ww ="world"; // Can change type.
Object ww2 ="world2"; // Can change type.
finalString smile =":)"; // Can only be assign a value Once.
constdouble pi =3.15; // Can't change data.
1
2
3
4
String name ="Dart";
int createDate =20250118;
double version =1.0;
bool wip =false;
switch (createDate) {
case1:case2:default:}
// Do not need [break;] in every case.
String mySwitch =switch (name) {
"Dart"=>"YEAH!",
"dart"=>"yeah",
_ =>"???", // Underscore is default.
};
bool mySwitch2 =switch (name) {
"Dart"||"dart"=>true,
_ =>false,
};
switch (myList) {
case [int a, int b]: print("[myList] has 2 int: $a,? $b");
case [1, _, _] when myList.last ==3:// Pattern matching, Guard clause.
print("[myList] starts with 1, ends with 3");
default:}
1
2
3
4
5
6
7
8
for (int i =0; i <3; i++) {}
for (intnumin myList) {}
while (false) {}
do {} while (false);
// [break;], [continue;]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
try {
throw Exception("Throw exception");
// throw "Throw arbitrary object";
// Choose one of the two lines above.
} on FormatException {
} on IOException catch (e) {
} catch (e) {
// Avoid using without [on].
if (false) rethrow;
} finally {
// [finally] always run regardless of [try]-[catch].
print("Do something after try-catch");
}
assert(true, "message");
// If [false], return [AssertionError] with ["message"].
Function
1
2
3
4
5
6
7
8
9
10
11
12
void myFunc1(int a, int b) {}
void myFunc2({requiredint a, requiredint b}) {} // Parameters have name.
void myFunc3(int a, [int b =1]) {} // Default value.
void myFunc4(int a, {requiredint b, int c =1}) {} // Name, default.
void myFunc5(int a, [int b =1, int? c]) {} // [c] is optional.
/// Arrow function
///
/// Just 1 line
/// Do not need [return].
var myFunc7 = () => print("Arrow Func");
void myFunc8() => print("Shorthand syntax");
Typedef
1
2
3
4
5
6
7
typedef FuncTypedef =int Function(int a, int b);
typedef ListTypedef = List<int>;
FuncTypedef typedef1 = (int a, int b) {
return a + b;
};
ListTypedef typedef2 = [1, 2, 3, ];
classMyClass {
finalString name; // Use final for inner value.
// Constructor
MyClass(this.name);
// MyClass(String name) {this.name = name;}
// MyClass(String name) : this.name = name;
// Named constructor
MyClass.noName() : name ="empty";
MyClass.noName2() :this("empty");
MyClass.nameWithAge(String name, int age) : name ="$name$age";
MyClass.nameAndDoSomething() : name ="This is initialize" {
print("Do something after initializing.");
}
void printName() {
print("MyClass name is ${name}");
}
}
1
2
3
4
var myInstance = MyClass("hello");
myInstance.printName(); // MyClass name is hello
var myInstance2 = MyClass.noName();
myInstance2.printName(); // MyClass name is empty
Private
Private class/variable/method name starts with _.
1
2
3
4
5
6
7
8
9
class_PrivateClass {
finalString _name;
_PrivateClass(this._name);
void _printName() {
print("My PrivateClass name is ${_name}");
}
}
abstractclassMyAbstract {
finalString name;
MyAbstract(this.name);
void printName(); // Abstract method.
void whoAmI() {
// If any codes are inside method,
// then that method is not abstract anymore.
print("I am not abstract method");
}
}
classChildAbstractextends MyAbstract {
ChildAbstract(super.name);
@override
void printName() {
print("Using abstract and override");
super.tellName();
}
}
Base
Child class should have [base], [final], [sealed].
1
2
3
base classMyBase {
MyBase();
}
Final
1
2
3
finalclassMyFinal {
MyFinal();
}
Interface
Every class is interface, so every class can use implements.
Interface can only has abstract methods.
1
2
3
interface classMyInterface {
MyInterface();
}
Sealed
1
2
3
sealed classMySealed {
MySealed();
}
Other Technic
DateTime
1
2
var now = DateTime.now();
var day = DateTime(2025, 1, 22); // Y: 2025, M: 1, D: 22
Generic
<T>: Any type var. T value.
<E>: Elements of list. List<E>
<K>: Keys. Map<K,V>
<V>: Values. Map<K,V>
1
2
3
4
5
6
classMyGeneric<T> {
final T name;
MyGeneric({requiredthis.name});
}
var temp = MyGeneric<int>(4);
Stream<int> sumStream(Stream<int> stream) async* {
var sum =0;
awaitfor (final value in stream) {
yield sum += value;
}
}
void sumCount() async {
Stream<int> timerStream = Stream.periodic(Duration(seconds: 1), (count) => count);
awaitfor (final cumulative in sumStream(timerStream)) {
print(cumulative);
}
}
// 0, 1, 3, 6, 10, 15, 21, 28
// Print these values one at a time every second.
void streamController() {
final controller = StreamController<int>();
controller.stream.listen((data) => print('Received: $data'));
for (int i =0; i <3; i++) {
controller.sink.add(i); // Add data manually.
}
controller.close(); // Close manually.
// Received: 0
// Received: 1
// Received: 2
controller.stream
.where((val) => val %2==0)
.listen((data) => print("even $data"));
// Can use any methods between [.stream] and [.listen]..
// This will be print "even 0", "even 2", ...
}
Isolate
import "dart:isolate";
Isolate is similar thing of multithread.
Isolate does not need sync.
Each isolate has own memory.
Isolates are communicate with async message: ReceivePort, SendPort.