skip to Main Content

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


  1. because the AppBar widget contains many properties that are not constant, so it can’t also be constant and so it can’t accept const.

    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 whole AppBar is not as well.

    For example, the actions property, accepts as an argument a List<Widget>? and that list can be changed dynamically, right ? so it is not constant, and so the AppBar 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.

    Login or Signup to reply.
  2. 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:

    preferredSize = 
              _PreferredAppBarSize(toolbarHeight, bottom?.preferredSize.height);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search