Why do I get an error when I try the following:
Widget build(BuildContext context) {
return const Scaffold(
appBar: AppBar(title: Text("Verify Email")),
body: Text("Needa verify that email dawg @_@"),
);
}
Error:
The constructor being called isn't a const constructor.
Try removing 'const' from the constructor invocation.
Why is the AppBar constructor not const?
2
Answers
because the
AppBar
widget contains many properties that are not constant, so it can’t also be constant and so it can’t acceptconst
.you can think about the
AppBar
widget, as a whole sub-tree of more other widgets, and if one of them is not contact, then the wholeAppBar
is not as well.For example, the
actions
property, accepts as an argument aList<Widget>?
and that list can be changed dynamically, right ? so it is not constant, and so theAppBar
is not.I hope you get the idea of what I’m trying to say, the AppBar is a sub-set of many properties that can not be constant, and this affects the
AppBar
.AppBar constructor isn’t constant (no const keyword leading its constructor) and it couldn’t because it have property with unknown value before compiling the program which: