Currently I'm assigning all variables through initState however I'm seeing that there is no need to assign the variables through initState as I can just assign the variable with a value directly. What is the order of these assignments and how are they different? Why and when would you choose one instead of the other?
class Person {
String name = "John";
@override
void initState(){
....
....
}
}
vs
class Person {
String name;
@override
void initState(){
name = "John";
}
}
In your first example, the assignment takes place during construction. You might want to use this form if name
is final
.
In the second example, the assignment takes place when initState
is called, which could be zero, one or more times. Presumably you are referring to the initState
of State<T>
which the framework calls once, after construction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With