Flutter initState에서 context 가져 오는 법

Chang Hyun Kim·2021년 3월 4일

일반적으로 initState 함수가 종료 되어야 위젯의 context를 가져올 수 있는데
Future.delayed 함수로 비동기 처리를 하면 initState에서 사용이 가능합니다.

@override
void initState() {
  super.initState();
  RatioHelper ratioHelper = RatioHelper();
  // Use Future for get context in initState
  Future.delayed(Duration.zero, () => ratioHelper.setup(context));
}
profile
https://kimbiyam.me/ 블로그 이전하였습니다

0개의 댓글