Skip to content

Instantly share code, notes, and snippets.

@junddao
Last active December 19, 2023 08:52
Show Gist options
  • Save junddao/459ea6b93965cc57db6ea24ad614e752 to your computer and use it in GitHub Desktop.
Save junddao/459ea6b93965cc57db6ea24ad614e752 to your computer and use it in GitHub Desktop.
dart 3.0_record
// record
// 순서와 타입을 보장해 주는 자료형
void main() {
final result = nameAndAge({
'name': '하하',
'age': 20,
});
print(result);
print(result.$1);
print(result.$2);
print('-------------');
final result2 = getUsersWithType();
for(final item in result2){
print(item.$1);
print(item.$2);
}
print('-------------');
final result3 = getUsersWithType2();
for(final item in result3){
print(item.$1);
print(item.$2);
}
print('-------------');
final result4 = getUsersWithType3();
for(final item in result4){
print(item.name);
print(item.age);
}
}
/// record
(String, int) nameAndAge(Map<String, dynamic> json) {
return (json['name'] as String, json['age'] as int);
}
// 전통적인 map 방식
List<Map<String, dynamic>> getUsers() {
return [
{
'name': '하하',
'age': 20,
},
{
'name': 'ghgh',
'age': 10,
},
];
}
// list record
List<(String, int)> getUsersWithType() {
return [
(
'하하',
20,
),
(
'ghgh',
10,
),
];
}
//list record with naming
List<(String name, int age)> getUsersWithType2(){
return [
(
'하하',
20,
),
(
'ghgh',
10,
),
];
}
//list record with named param
List<({String name, int age})> getUsersWithType3(){
return [
(
name: '하하',
age :20,
),
(
name: 'ghgh',
age: 10,
),
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment