How does setState() rebuild child widgets?

前端 未结 2 1068
故里飘歌
故里飘歌 2021-01-05 02:52

I just need some idea on how flutter stateful widgets build their stateful children when setState() is invoked. Please look at the code below.



        
2条回答
  •  灰色年华
    2021-01-05 03:41

    When you use setState(()) only the build() will get called. Now in this method you are again invoking StatefulChild() (first line) which further instantiates this class so the build() method of this class gets executed.

    But when you use statefulChild it is not creating a new instance because

    StatefulChild statefulChild = StatefulChild();
    

    is used outside the build() method. So it just got called once.

提交回复
热议问题