튜트리얼 class 문제까지 할 수 있는 문제는 하고 넘어갔습니다.
다음장부터 하는데 아예 처음 접한 내용이여서 속도가 안났습니다.
머리 꽁꽁 싸매다가
OOP까지 정독하고 다시 처음으로 돌아가 복습하고 다시 넘어가면 좋겠다
라는 생각으로 복습을 시작했습니다.
문제를 풀면서 벨로그에 올렸던 nested 문제
student 라는 Map type 변수를 만들어라.
변수는 name, id, grade 라는 key를 갖는다.
name=Jane, id=12345 값을 갖는다.
grade의 값은 Map type이다. math와 history란 key를 갖고,
math=3, history=4.3 값을 갖는다.
history의 학점을 console에 출력하라.

void main() {
Map<String, dynamic> student = {
'name': 'Jane',
'id': 12345,
'grade': {'math': 3, 'history': 4.3}
};
print('history 과목 ${student['grade']['history']}');
}
출력의 history 도 ${student['grade']['history']}
'처럼 작성 할 수 있지 않을까?' 궁금해서 수정하기 시작했죠.
print('${student['grade']} 과목 ${student['grade']['history']}');

여기서 '${student['grade'][1]} 을 하면
{'history': 4.3} 과목 이 출력 되겠지?' 생각했는데

null?


null이 왜 나오는 문제는 다음에 작성하기로 하고


type '_Map<String, num>' is not a subtype of type 'Iterable'
'_Map<String, num>' 유형은 'Iterable' 유형의 하위 유형이 아닙니다.
for문에 Map<String, num> 안되는 것을 알고 있지만
오류줄이 없어서 궁금해서 찍어봤습니다.
안된다고 생각한 이유는
'math': 3 , 'history': 4.3 key:value 값이
차례대로 subject에 들어가는게 이상하다고 생각했습니다.
for (var subject in student['grade'].keys) {
print('$subject 과목 ${student['grade']['history']}');
}

student['grade'].keys 로 키 값만 subject 들어가고
['grade'].keys 는 math , history 로 인해
for로 돌리면 2번 나오기 때문에
for (var subject in student['grade'].keys) {
if (subject == 'history') {
print('$subject 과목 ${student['grade']['history']}');
}
}
if 문으로 history 일 때 해당 과목만 출력 할 수 있게 코드를 작성했습니다.

한달전에는 못 풀었지만 이제는 해결했다!!
// 전체 코드
void main() {
Map<String, dynamic> student = {
'name': 'Jane',
'id': 12345,
'grade': {'math': 3, 'history': 4.3}
};
for (var subject in student['grade'].keys) {
if (subject == 'history') {
print('$subject 과목 ${student['grade']['history']}');
}
}
}
그래도 앞으로 갈 길이 멉니다...
멀지만 화이팅 !!
화이팅~