I am stuck with gasp about const constructor in Dart (Flutter).
Supposed that I have StatelessWidget with const constructor
class ChildWidget extends StatelessWidget {
final String myText;
const ChildWidget(
{this.myText = ''});
@override
Widget build(BuildContext context) {
return Text(
myText
);
}
}
And using it in parent widget
class _ParentWidgetState extends State<ParentWidget> {
String myText = 'Hello World';
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
const ChildWidget(myText: myText),
],
),
),
);
}
}
It shows error that myText is invalid const. But when I remove const keyword precede ChildWidget, everything will be OK. What happens? What are differences between have and does not have const keyword?
I supposed that const keyword will trigger const constructor in ChildWidget. Maybe Flutter implicitly creates normal constructor with the same argument such as const one and trigger this when not having const keyword?
Please explain it to me. Many thanks and have a good day.