Reputation: 702
I got a Visual Studio warning on my class (below) saying "This class (or a class which this class inherits from) is marked as '@immutable', but one or more of its instance fields are not final: UserSignIn._email", but I cannot mark this argument as final because I initialise it in the constructor
Without final :
class UserSignIn extends StatefulWidget {
TextEditingController _email;
UserSignIn({String emailInput}) {
this._email = TextEditingController(text: (emailInput ?? ""));
}
@override
_UserSignInState createState() => _UserSignInState();
}
class _UserSignInState extends State<UserSignIn> {
@override
Widget build(BuildContext context) {
...
}
}
How to do this ?
Thank you
Upvotes: 0
Views: 1518
Reputation: 873
You should put the TextEditingController in the state class and initialise it in the initState method, like this.
And keep in mind that the StatefulWidget can be different every time when the widget tree is changed, so don't put anything in there that is not immutable. Keep everything dynamic in the State class
class UserSignIn extends StatefulWidget {
final String emailInput;
const UserSignIn({Key key, this.emailInput}) : super(key: key);
@override
_UserSignInState createState() => _UserSignInState();
}
class _UserSignInState extends State<UserSignIn> {
TextEditingController _controller;
@override
void initState() {
_controller = TextEditingController(text: widget.emailInput);
super.initState();
}
...
}
Upvotes: 4