我知道每次调用setState
都会调用build
,我想在flutter框架中找到方法链。
所以我检查了flutter的源代码,找到了setState
的一些步骤:
_element!.markNeedsBuild()
将state的元素标记为dirty。
owner!.scheduleBuildFor(this)
调用BuildOwner
的onBuildScheduled
并将元素添加到脏元素列表。BuildOwner
的onBuildScheduled
被分配到WidgetsBinding
的initInstances()
中:
1.而WidgetsBinding
的initInstances()
在runApp()
中被调用。
我已经仔细检查过了,但是没有看到任何代码调用state
的build
方法。setState
如何在flutter内部调用build
?我们能找到源代码吗?
1条答案
按热度按时间h7appiyu1#
在
handleDrawFrame()
中,您可以看到以下部分:我不知道它被添加到
persistenCallbacks
的确切位置,但是查看代码中的变量可以看到这意味着它调用
drawFrame
在
drawFrame
中,你会发现这行代码:在这个方法中,它查看
_dirtyElements
中的所有元素。请注意,在scheduleBuildFor
中,你在这个问题中发布了它。在这个buildScope
中,它调用这反过来又叫
对于
ComponentElements
来说