Error: Cannot invoke a non-'const' constructor where a const expression is expected
Solution 1
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.
Solution 2
Remove the first two words: "static const" from this:
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Startseite',
style: optionStyle,
),
BestRazer
Updated on December 21, 2022Comments
-
BestRazer 1 day
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, ), ];