I have a stateless widget and while writing the code I am using a non-final field in the stateless widget and the ide keeps giving me warning that all the fields in stateless widget should be final
But I don't understand why having a non-final field in stateless widget be a problem.
I think it should be perfectly fine to have non-final field because there could be a field that we don't want to modify later but this field can only be initialized inside the constructor function so, for that you need to use non-final field
example:
class Temp extends StatelessWidget {
final int a;
final int b;
int c;
temp({this.a, this.b}) {
this.c = this.a + this.b;
}
@override
Widget build(BuildContext context) {}
}
In the above widget, I can't make c
as final because it is initialized inside the constructor function even though I have no plans to change the c
variable in the future.
If having a non-final field in a Stateless widget is not a good Idea then How to handle the above situation.
Note: I cannot use the Constructor() : [initialization] {}
because the initialization can involve the function or loops