java 비슷
bool isDone = true;
List items = [1,2,3, if(isDone) 99];
items.first();
items.last();
items.isEmpty();
items.length;
for(int item in items) {}
List addItem = [...items, 4, 5,6]; // spread 연산자
Map
Map<String, dynamic> men = {
'name' : 'john',
'age' : 40
};
men('name') = 'kitty'; // update
print(men.keys); // name, age
print(men.entries);
print(men.values);
for( var key in men.keys) {
print(men[key]);
}
for(var entry in men.entreis) {
print('${entiry.key}, ${entiry.value}');
}
class
void main() {
var point1 = Point(1, 2);
point1.move();
print(point1.x);
porint1.y = 100;
print(Point.glovalName); // static 이므로
Point.glovalName = 'yourName';
Point point3 = Point.fromMap({ 'x':10, 'y':20});
}
String newGlovalText = 'allRound'; // 어디에서든 쓸 수 있어서
// static보다 편리함
class Point {
int x=0;
int y=0;
int z=0; // 붙이면 private가 됨
int? t;
late int s; // late 지연 초기화
static String glovalName ='myName;
Point(this.x; this.y, this._z, [this.t]); // t를 넣어도 되고 안넣어도 됨
void move() {
print('move');
}
static void test() {
print('test');
}
// named constructor
Point.fromMap(Map map) {
_x = map['x'];
_y = map['y'];
}
}
상속
자바와 비슷
mixin : 다중상속
void main() {
}
mixin Moveable {
int i=0;
}
SuperHero sh extends Hero with Moveable ...
generic
class Box {
T value;
}
void main() {
var box = Box(11);
var box2 = Box('strbox');
}
import
import 'dart:async' as async;
import 'dart:async' hide AsyncError;
import 'dart':aync' show AsyncError;
Future : 비동기 처리
var delay = Future.delayed(Duration(seconds:3));
delay.then((value) => print('3seconds delay')
.catchError((e) => print(e));
delay.whenComplete(...)
Future hello() async {
await Fure.delayed(Duration(seconds:3));
return 'hello';
}
hello.then((value) => print(value));
stream : data 흐름
var stream =STream.fromIterable([1,2,3]);
stream.listen((event) {
print(event);
});