Widget build(BuildContext context) {
// TODO: Instead of re-creating a list of Categories in every build(),
// save this as a variable inside the State object and create
// the list at initialization (in initState()).
// This way, you also don't have to pass in the list of categories to
// _buildCategoryWidgets()
final categories = <Category>[];
...
2 回答
这实际上恰恰相反 .
在许多情况下可以再次调用
build
. 如状态更改或父级重建 .而
initState
只被召唤一次 .build
应仅用于布局 . 而initState
通常用于变量初始化 .它位于您提供的link的构建状态中的注释中 .
在构建状态中创建类别列表将导致在每个构建上创建列表 . 这是必要的,因为你只想创建一次,所以最好的地方是initState(),因为它只会在创建状态对象时调用一次,从而消除了重新创建类别的成本 . 每个构建 .
根据flutter doc:
InitState
build