英文:
is there any difference between assigning value to the variable inside of initState or not in Flutter StatefulWidget?
问题
- 使用值初始化变量 (firstCase)
- 不使用值初始化变量,而在initState内部赋值给变量 (secondCase)
这两种方式之间有区别吗?
或者在实践中哪一种更好?
英文:
I saw two ways to declare variables with StatefulWidget in many sample codes.
- initialize variable with value (firstCase)
- initialize variable without value and assign to value inside of initState (secondCase)
Is there any difference between these?
Or which one would be better code in practice?
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,
);
}
}
答案1
得分: 35
如果您可以直接在属性中初始化变量,请这样做。这对于可读性更好(一个地方查找)。
唯一需要使用 initState
的原因是当您 无法 直接从声明中初始化变量时。
这些情况在大多数情况下是这样的:
- 您的变量依赖于
widget
或context
- 它依赖于
this
例如,如果您想创建一个 AnimationController
,您需要传递 vsync: this
。但以下内容将不会编译:
class MyState extends State with SingleTickerProviderStateMixin {
final myController = AnimationController(
vsync: this, // 编译错误,无法在初始化器中使用 `this`
);
}
相反,您必须这样写:
class MyState extends State with SingleTickerProviderStateMixin {
AnimationController myController;
@override
void initState() {
super.initState();
myController = AnimationController(
vsync: this, // OK
);
}
}
尽管请注意,这个特定的示例将很快发生变化,因为Dart的未来版本将引入 late
关键字,然后允许:
class MyState extends State with SingleTickerProviderStateMixin {
late final myController = AnimationController(
vsync: this, // OK,这次不会编译错误
);
}
但对于依赖于 widget
/context
的变量,您可能仍然需要使用 initState
。
英文:
If you can create initialise your variable directly in the property, do so. It's better for readability (a single place to look for).
The only reason you'll want to use initState
is for when you cannot initialise the variable directly from its declaration.
These situations are for the most part:
- your variable depends on
widget
orcontext
- it depends on
this
For example, if you want to create an AnimationController
you'll need to pass it vsync: this
. But the following won't compile:
class MyState extends State with SingleTickerProviderStateMixin {
final myController = AnimationController(
vsync: this, // compile error, cannot use `this` on initialisers
);
}
And you'd have to instead write:
class MyState extends State with SingleTickerProviderStateMixin {
AnimationController myController;
@override
void initState() {
super.initState();
myController = AnimationController(
vsync: this, // OK
);
}
}
Although note that this specific example will soon change as a future version of Dart will introduce the late
keyword, which then allows:
class MyState extends State with SingleTickerProviderStateMixin {
late final myController = AnimationController(
vsync: this, // OK, not a compile error this time
);
}
You may still need initState
for variables that depends on widget
/context
though.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论