Passing Data to a Stateful Widget. Don’t pass parameters to State using it’s constructor. You should only access these using this.widget.myField.
Not only editing the constructor requires a lot of manual work ; it doesn’t bring anything. There’s no reason to duplicate all the fields of Widget.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class ServerIpText extends StatefulWidget { final String serverIP; const ServerIpText ({ Key key, this.serverIP }): super(key: key); @override _ServerIpTextState createState() => _ServerIpTextState(); } class _ServerIpTextState extends State<ServerIpText> { @override Widget build(BuildContext context) { return Text(widget.serverIP); } } class AnotherClass extends StatelessWidget { @override Widget build(BuildContext context) { return Center( child: ServerIpText(serverIP: "127.0.0.1") ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.