[Flutter] 5. Dart 3.0 update

BeanxxΒ·2024λ…„ 7μ›” 26일
0

Flutter

λͺ©λ‘ 보기
5/5
post-thumbnail

βœ…Β Record

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),
	];
}

βœ…Β Destructuring

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

βœ…Β Pattern Matching

// 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

  1. mixin은 extendsλ‚˜ with μ‚¬μš© λΆˆκ°€ βŒΒ β†’ κ·ΈλŸ¬λ―€λ‘œ mixin class도 μ‚¬μš© λΆˆκ°€ ❌
  2. ν΄λž˜μŠ€λŠ” on ν‚€μ›Œλ“œ μ‚¬μš© λΆˆκ°€ ❌ β†’ κ·ΈλŸ¬λ―€λ‘œ mixin class도 on ν‚€μ›Œλ“œ μ‚¬μš© λΆˆκ°€ ❌
mixin class AnimalMixin {
	String bark() {
		return '멍멍';
	}
}

class Dog with AnimalMixin {}
profile
FE developer

0개의 λŒ“κΈ€