going through some flutter code and discover this flutter code
const _validatedFormzStatuses = <FormzStatus>{
FormzStatus.valid,
FormzStatus.submissionInProgress,
FormzStatus.submissionSuccess,
FormzStatus.submissionFailure,
FormzStatus.submissionCanceled,
};
can anyone share how this constant work? the curly bracket part, and the generic type. also any keyword i can use to find answer in the web? am struggling to determine the term of this constant format, thanks.
CodePudding user response:
This const refers that's once u initialled any value to that variable then u can't change the value of that constants variables in future. For Eg. A man have only 2 Hands and u can not changed man hands number.
so that's why we are using const in flutter or any programming language.
CodePudding user response:
consider we have these three lines of code:
1.const EdgeInsets.all(25.0)
2.const EdgeInsets.all(25.0)
3.const EdgeInsets.all(25.0)
At first line EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget and then it creates a constant object with same value for rendering if it is found in another place.
Hey there is already an object with this value, so just render it.
Hey there is already an object with this value, so just render it.
Now, let us consider these scenario:
1.EdgeInsets.all(25.0)
2.EdgeInsets.all(25.0)
3.EdgeInsets.all(25.0)
At first line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
At the second line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
At third line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
So, by using const we can reduce the time for recreating the same object every time and using it, instead, we create an object once and then reuse it at every time we need.
