final result = nameAndAge({
'name': 'νλ',
'age': 20,
});
print(result); // (νλ, 20) -> tuple: νμ
μμ 보μ₯
print(result.$1); // νλ
print(result.$2); // 20
// ------------------------
// Record: type & type μμ 보μ₯ λ°μ μ μμ
(String, int) nameAndAge(Map<String, dynamic> json) {
return (json['name'] as String, json['age'] as int);
}
// 1
List<Map<String, dynamic>> getNewJeans() {
return [
{'name': 'νλ', 'age': 20},
{'name': 'λ―Όμ§', 'age': 21},
];
}
// 2
List<(String, int)> getNewJeansWithType() {
return [
('νλ', 20),
('λ―Όμ§', 21),
];
}
final result3 = getNewJeansWithType();
for (final item in result3) {
print(item.$1);
print(item.$2);
}
// 3
List<({String name, int age})> getNewJeansWithType2() {
return [
(name: 'νλ', age: 20),
(name: 'λ―Όμ§', age: 21),
];
}
final (name, age) = ('νλ', 20);
print(name); // νλ
print(age); // 20
final newJenas = ['νλ', 'λ―Όμ§'];
final [String a, String b] = newJeans;
print(a); // νλ
print(b); // λ―Όμ§
final numbers = [1, 2, 3, 4, 5, 6, 7, 8];
final [x, y, ..., z] = numbers;
// ... -> μ€κ° κ°λ€μ λ¨Ήμ΄λ²λ¦¬κ³ , ν κ³³μλ§ μ¬μ© κ°λ₯
final [xx, yy, ...rest, zz] = numbers;
// ...rest: λ¨Ήμ΄λ²λ¦¬λ κ°λ€μ μ΄λ¦μ μ§μ ν΄μ£Όλ©΄ λ€λ₯Έ κ³³μμ κ°μ Έλ€ μ¬μ© κ°λ₯
print(xx); // 1
print(yy); // 2
print(zz); // 8
print(rest); // [3, 4, 5, 6, 7]
// ---------------------------------------------
// _ : κ° μμ ν 무μ
final [xxx, _, yyy, ...rest2, zzz, _] = numbers;
print(xxx); // 1
print(yyy); // 3
print(zzz); // 7
print(rest2); // [4, 5, 6]
final haniMap = {'name': 'νλ', 'age': 20};
final {'name': NAME, 'age': AGE} = haniMap;
print(NAME); // νλ
print(AGE); // 20
class Idol {
final String name;
final int age;
Idol({
required this.name,
required this.age,
})
}
final haniIdol = Idol(name: 'νλ', age: 20);
final Idol(name: NAME2, age: AGE2) = haniIdol;
print(NAME2); // νλ
print(AGE2); // 20
// validation
final hani = ('νλ', 20);
final (name as String, age as int) = hani;
print(name); // νλ
print(age); // 20
void switcher(dynamic anything) {
switch(anything) {
case 'aaa':
print('match: aaa');
case ['1', '2']:
print('match: [1, 2]');
case [_, _, _]:
print('match: [_, _, _]');
case [int a, int b]:
print('match: [int $a, int $b]');
case < 10 && > 5:
print('match: < 10 && > 5');
default:
print('no match');
}
}
switcher('aaa'); // match: aaa
switcher([1, 2]); // no match
switcher([1, 2, 3]); // match: [_, _, _]
switcher(7); // match: < 10 && > 5
// ----------------------------------
String switcher2(dynamic val, bool condition) => switch (val) {
5 => 'match: 5',
7 when condition => 'match 7 and true',
_ => 'no match', // default
}
print(switcher2(5, true)); // match: 5
print(switcher2(7, true)); // match 7 and true
print(switcher2(7, false)); // no match
void forLooper() {
final List<Map<String, dynamic>> members = [
{'name': 'νλ', 'age': 20},
{'name': 'λ―Όμ§', 'age': 21},
];
for (final member in members) {
print(member['name']);
print(member['age']);
}
// === μ½λ λμ λμΌ
for (var {'name': NAME, 'age': AGE} in members) {
print(NAME);
print(AGE);
}
}
void ifMatcher() {
final hani = { 'name': 'νλ', 'age': 20 };
if (hani case {'name': String name, 'age': int age}) {
print(name);
print(age);
}
}
final
λ‘ ν΄λμ€ μ μΈνλ©΄ extends
, implement
, mixin
μΌλ‘ μ¬μ© λΆκ°
base
λ‘ μ μΈνλ©΄ extend
κ°λ₯ βοΈ, implement
λΆκ°λ₯ β
base class person {
final String name;
final int age;
Person({
required this.name,
required this.age,
});
}
interface
λ‘ μ μΈνλ©΄ implement
λ§ κ°λ₯
sealed
ν΄λμ€: abstract
μ΄λ©΄μ final
/ ν¨ν΄λ§€μΉ μ¬μ©ν μ μλλ‘ ν΄μ€
Mixin Class
mixin
μ extends
λ with
μ¬μ© λΆκ° βΒ β κ·Έλ¬λ―λ‘ mixin class
λ μ¬μ© λΆκ° βon
ν€μλ μ¬μ© λΆκ° β β κ·Έλ¬λ―λ‘ mixin class
λ on
ν€μλ μ¬μ© λΆκ° βmixin class AnimalMixin {
String bark() {
return 'λ©λ©';
}
}
class Dog with AnimalMixin {}