dart (2) 썸네일형 리스트형 [dart] 타입 캐스팅, 옵셔널 체이닝 | type casting + optional chaining type 'A' is not a subtype of type 'B' 아래와 같은 오류가 뜬다. final List authors = json['authors'] // 에러 => type 'List' is not a subtype of type 'List' json['authors'].runtimeType 을 로깅해서 봤더니 List 이라고 떴다. 만약 실제로 String 타입이 오고 dart가 이를 모르는 상황이라면 타입 캐스팅을 위해 아래처럼 바꿔주면 된다. (하지만 타입이 정말 다른 경우는 똑같은 에러가 발생할 것이다.) optional chaining optional chaining은 null이 올 수 있는 객체라면, null이 아닐 때만 그 안의 method나 값을 꺼낼 수 있게 해주는 것이다. .. [dart] what is factory? | 싱글톤 패턴 singleton pattern class what is factory? flutter에서 모델 클래스를 만들 때 아래 같은 코드가 자주 사용된다. 그런데 factory라는 것을 dart에서 처음 봤다. factory 키워드는 무엇이고 어떤 역할을 할까? class Book { final String title; final String description; Book({this.title, this.description}); factory Book.fromJson(Map json) { return Book( title: json['title'], description: json['description'], ); } } singleton pattern Use the factory keyword when implementing a constructo.. 이전 1 다음