Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot invoke a non-'const' constructor where a const expression is expected

Tags:

flutter

dart

I've took the "BottomNavigationBar" example to build my app on it. I've tried to implement a RaisedButton like this:

      child: Container(
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text('Montag'),
            )
          ],
        ),
      ),
    ), 

and get the following error message:

lib/main.dart:48:16: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
       child: Column(
              ^^^^^^
lib/main.dart:47:14: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
     child: Container(
            ^^^^^^^^^

What can I do? I know thats probably a bad question but any help is appreciated.

Edit: As requested, here is more code:

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
  TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Startseite',
      style: optionStyle,
    ),
    const Center(
      child: const Container(
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text('Montag'),
            )
          ],
        ),
      ),
    ),
    const WebView(
      initialUrl: 'mygoogledrivelink',
      javascriptMode: JavascriptMode.unrestricted,
    ),
  ];
like image 577
BestRazer Avatar asked Jul 19 '20 17:07

BestRazer


2 Answers

You're not showing it but you surely have a const widget constructor higher in the widget tree, that causes any child widget to require to be constructed with const.

Unfortunately not all widgets has a const constructor, Container and Column are two examples of that. You won't be able to construct those widgets as child of a const constructor.

You could either try to find replacement for all the widgets requiring a const constructor or remove the const keyword in the parent widget.

like image 165
Claudio Redi Avatar answered Nov 02 '22 14:11

Claudio Redi


Remove the first two words: "static const" from this:

 static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Startseite',
      style: optionStyle,
    ),
like image 3
Michael Tran Avatar answered Nov 02 '22 15:11

Michael Tran