相关疑难解决方法(0)

在 initState 中为变量赋值与在 Flutter StatefulWidget 中不赋值有什么区别吗?

我在许多示例代码中看到了两种使用 StatefulWidget 声明变量的方法。

  1. 用值初始化变量(firstCase
  2. 初始化没有值的变量并分配给 initState ( secondCase )内的值

这些有什么区别吗?或者哪一个在实践中是更好的代码?

class Sample extends StatefulWidget {
  Sample({Key key}) : super(key: key);

  @override
  _SampleState createState() => _SampleState();
}

class _SampleState extends State<Sample> {
  bool firstCase = false;
  bool secondCase;

  @override
  void initState() {
    secondCase = false;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: child,
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

dart flutter

11
推荐指数
1
解决办法
877
查看次数

标签 统计

dart ×1

flutter ×1