배 타다 개발자

플러터 위젯 생명주기 (Flutter Widget LifeCycle) 본문

카테고리 없음

플러터 위젯 생명주기 (Flutter Widget LifeCycle)

노 아 2022. 2. 26. 08:37

위젯 생명주기(Life Cycle)

createState()
When the Framework is instructed to build a StatefulWidget, it immediately calls createState()

class MyHompate extends StatefulWidget
{
@override
_MyHomepateState createState() => new _MyHomePageState();
}

mounted is true
When createState creates your state class, a buildContext is assigned to that state. buildContext is, overly simplified, the place in the widget tree in which this widget is placed. Here's a longer explanation. All widgets have a bool this.mounted property. It is turned true when the buildContext is assigned. It is an error to call setState when a widget is unmounted.

if (mounted)
{
    setState();
}

initState()
This is the first method called when the widget is created (after the class constructor, of course.) initState is called once and only once. It must call super.initState().

State에서 제일 먼저 실행되는 함수, 주로 데이터 목록을 만들거나 처음 필요한 데이터를 주고받을 때 호출

@override
initState()
{
 super.initState();
 _getJsonDate();
 }

didChangeDependencies()
This method is called immediately after initState on the first time the widget is built.

build()
This method is called often. It is required, and it must return a Widget.

위젯을 랜더링하는 함수

Widget build(BuilContext context){
  return MaterialApp(
    title: 'Flutter Demo';,
    theme: ThemeData(
    primarySwatch: Color.amber,
    ),
    home: MyHoomePage(title: ' Demo Home'),
  );
}

didUpdateWidget(Widget oldWidget): If the parent widget changes and has to rebuild this widget (because it needs to give it different data), but it's being rebuilt with the same runtimeType, then this method is called. This is because Flutter is re-using the state, which is long lived. In this case, you may want to initialize some data again, as you would in initState.

initState()함수는 생성될 때 한번만 호출되므로 이후에 위젯에 변경사항이 생길 때는

didUpdateWidget(Widget oldWidget)

함수를 호출하여 변경사항을 갱신한다.

@override
void didUpdateWidget(Widget oldWidget){
    if(oldWidget.importantProperty != widget.importantProperty)
    {
    _init();
    }
}

위젯을 변경해야 할 때 호출하는 함수

setState()
This method is called often from the framework itself and from the developer. Its used to notify the framework that data has changed

데이터가 변경되었음을 알리는 함수, 변경된 데이터를 UI에 적용하기 위해 필요하다.

void updateProfile(String name)
{
    setSate(() => this.name = name));
}

deactivate()
Deactivate is called when State is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists basically because State objects can be moved from one point in a tree to another.

위젯의 상태관리를 중지
State가 제거될 때 호출, 아직 메모리상에서는 제거되지 않았다.

dispose(): dispose() is called when the State object is removed, which is permanent. This method is where you should unsubscribe and cancel all animations, streams, etc.

State가 완전히 제거 되었을 때 호출

mounted is false
The state object can never remount, and error will be thrown if setState is called.

모든 프로세서가종료된 후 mounted가 flase