source: google image
type 'A' is not a subtype of type 'B'
์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฌ๋ค.
final List<String> authors = json['authors']
// ์๋ฌ
=> type 'List<dynamic>' is not a subtype of type 'List<String>'
Dart
๋ณต์ฌ
json['authors'].runtimeType ์ ๋ก๊น
ํด์ ๋ดค๋๋ List<dynamic> ์ด๋ผ๊ณ ๋ด๋ค. ๋ง์ฝ ์ค์ ๋ก String ํ์
์ด ์ค๊ณ dart๊ฐ ์ด๋ฅผ ๋ชจ๋ฅด๋ ์ํฉ์ด๋ผ๋ฉด ํ์
์บ์คํ
์ ์ํด ์๋์ฒ๋ผ ๋ฐ๊ฟ์ฃผ๋ฉด ๋๋ค.
(ํ์ง๋ง ํ์
์ด ์ ๋ง ๋ค๋ฅธ ๊ฒฝ์ฐ๋ ๋๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.)
optional chaining
optional chaining์ null์ด ์ฌ ์ ์๋ ๊ฐ์ฒด๋ผ๋ฉด, null์ด ์๋ ๋๋ง ๊ทธ ์์ method๋ ๊ฐ์ ๊บผ๋ผ ์ ์๊ฒ ํด์ฃผ๋ ๊ฒ์ด๋ค.
์๋ฅผ ๋ค์ด ํ์
์บ์คํ
ํ๋ ค๋ ๊ฐ์ฒด๊ฐ null์ด ๋ ์ ์๋ค๋ฉด, ๊ฐ์ฒด์ optional chaining ?. ์ ๋ถ์ฌ null์ด ์๋ ๊ฒฝ์ฐ์ cast ํจ์๊ฐ ์คํ๋๋๋ก ํ ์ ์๋ค.
final List<String> authors = json['authors']?.cast<String>()
Dart
๋ณต์ฌ

