is there any difference between assigning value to the variable inside of initState or not in Flutter StatefulWidget?

huangapple go评论71阅读模式
英文:

is there any difference between assigning value to the variable inside of initState or not in Flutter StatefulWidget?

问题

  1. 使用值初始化变量 (firstCase)
  2. 不使用值初始化变量,而在initState内部赋值给变量 (secondCase)

这两种方式之间有区别吗?
或者在实践中哪一种更好?

英文:

I saw two ways to declare variables with StatefulWidget in many sample codes.

  1. initialize variable with value (firstCase)
  2. 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 的原因是当您 无法 直接从声明中初始化变量时。

这些情况在大多数情况下是这样的:

  • 您的变量依赖于 widgetcontext
  • 它依赖于 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 or context
  • 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.

huangapple
  • 本文由 发表于 2020年1月6日 20:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定