Search
🚧

flutter type casting, optional chaining

subtitle
cast() 사용, optional chaining
Tags
flutter
draft
Created
2021/02/18
2 more properties
아래 오류가 뜬다.
final List<String> authors = json['authors'] // 에러 => type 'List<dynamic>' is not a subtype of type 'List<String>'
Dart
복사
json['authors'].runtimeType 을 로깅해서 보면 List<dynamic> 이라고 뜬다.
타입 캐스팅을 위해 아래처럼 바꿔주면 된다.
final List<String> authors = json['authors']?.cast<String>()
Dart
복사
javascript 처럼 객체에 optional chaining ?. 을 붙여 null이 아닌 경우에 cast 함수가 실행되도록 할 수 있다.