Typedef
typedef ListOfInts = List<int>; // typescript interface or type like
ListOfInts reverseListOfNumbers(ListOfInts list) {
var reversed = list.reversed;
return reversed.toList();
}
Class
late로 constructor 실행시 args 받아서 전역필드에 저장
class Player {
late final String name;
late int xp;
Player(String name, int xp) {
this.name = name;
this.xp = xp;
}
void sayHello() {
print('I\'m $name');
}
}
간단하게
class Player {
final String name;
int xp;
// 순차적으로 args 받는다. positional arguments
Player(this.name, this.xp);
void sayHello() {
print('I\'m $name');
}
}
Named Constructor Parameter !!
class Player {
final String name;
int xp;
String team;
int age;
//required 문구 추가 또는 기본값 =
Player({
this.name = 'name',
required this.xp,
required this.team,
required this.age,
});
void sayHello() {
print('I\'m $name');
}
}
void main() {
var player = Player(
// name: 'Son',
xp: 1000,
team: 'red',
age: 12,
);
print(player.name);
player.sayHello();
}
Named & positional arguments Constructor
: 의 위치 중요
...
// named
Player.createBluePlayer({required String name, required int age})
: this.age = age,
this.name = name,
this.team = 'blue',
this.xp = 0;
// positional
Player.createRedPlayer (String name, int age)
: this.age = age,
this.name = name,
this.team = 'red',
this.xp = 0;
...
void main() {
var player1 = Player.createRedPlayer(
'Sonny',
12,
);
var player2 = Player.createBluePlayer(
name: 'Son',
age: 12,
);
}