我采用“BottomNavigationBar”示例在其上构建我的应用程序。我试图实现一个 RaisedButton 像这样:
child: Container(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('Montag'),
)
],
),
),
),
并收到以下错误消息: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(
^^^^^^^^^
我能做什么?我知道这可能是一个不好的问题,但任何帮助表示赞赏。
编辑:根据要求,这里有更多代码:
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,
),
];
最佳答案
您没有显示它,但您肯定在小部件树中具有更高的 const 小部件构造函数,这会导致任何子小部件都需要使用 const
构造。
不幸的是,并非所有小部件都有 const
构造函数,Container
和 Column
就是其中的两个示例。您将无法将这些小部件构造为 const
构造函数的子代。
您可以尝试为所有需要 const
构造函数的小部件寻找替代品,也可以删除父小部件中的 const
关键字。
关于flutter - 错误 : Cannot invoke a non -'const' constructor where a const expression is expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62983305/