본문 바로가기

개발 이야기/flutter

(5)
[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나 값을 꺼낼 수 있게 해주는 것이다. ..
[flutter] graphql code generator | artemis 사용 방법과 장단점 model, fromJson 노가다 flutter에서 model 클래스 정의하고, json serializer를 일일이 만들어줘야하는 게 불편했다. 데이터를 가져올 때마다 매번 이래야하니 귀찮아 죽을 것 같았다. 🤯 두번째 모델을 만들려다가 flutter에도 왠지 graphql-code-generator 같은 비슷한 라이브러리가 있을 것 같았다.아니나 다를까 3개 정도가 있었는데 그 중 artemis가 document도 잘 정리되어있고 star 수도 많아 쓸만해보였다. Artemis 아르테미스는 graphql 파일을 찾아 dart 파일을 생성해주는 code generator 기능이 있고 ArtemisClient로 graphql 쿼리도 날릴 수 있다. artemis는 아폴론(apollo)의 쌍둥이 여동생인..
[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..
[flutter] 불필요한 rerendering을 피하는 방법 | AutomaticKeepAliveClientMixin state 변경되어도 rerendering 하지 않는 방법 아래 그림은 StatefulWidget의 생명주기(life cycle)이다. build 메서드는 StatefulWidget에서 화면 상에 보여줄 위젯을 그린다. build 메서드는 initState를 실행하거나, 부모 위젯에 의해 config가 업데이트 되었거나, setState로 인해 state가 변경되었을 때 실행된다. 예를 들어 2개의 탭 위젯이 있다고 했을 때, 다른 탭으로 라우팅하면 기존에 그렸던 위젯은 dispose된다. 돌아올 때 다시 build되면서 불필요하게 rerendering하고 탭을 바꿀 때마다 깜빡거리는 현상이 발생한다. 라우팅될 때마다 dispose되어 발생하는 불필요한 rerendering을 막으려면 StatefulWi..
[Flutter] 자식 위젯에서 부모 위젯 state 참조하는 방법 | findAncestorStateOfType 부모 위젯의 state를 참조, 변경하는 법 자기 자신의 state를 업데이트하려면 setState를 쓰면 된다. 하지만 자식이 부모 위젯의 state를 변경하려면 어떻게 해야할까? How to Set/Update State of StatefulWidget from other StatefulWidget in Flutter? For Example in the below code plus button works and able to update the text but the minus button does not. But if we press FloatingActionButton then the State is refreshed . The minus button is cha... stackoverflow.c..