我以"BottomNavigationBar"为例来构建我的应用程序.我曾try 实现如下的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构造函数,ContainerColumn就是其中的两个例子.您将无法将这些小部件构造为const构造函数的子部件.

您可以try 为所有需要const构造函数的小部件寻找替代品,也可以删除父小部件中的关键字const.

Dart相关问答推荐

捕获一个区域中的所有期货

Dart中使用set表达式的switch语句

为什么使用三元条件运算符会产生return_of_invalid_type错误?

Flutter 布局:如何在 Flutter 中将 Row 放入 Flex(或另一个 Row)中?还使用堆栈小部件

浮动操作按钮:如何像 RaisedButton 一样更改启动 colored颜色 和突出显示 colored颜色 ?

Flutter tabview刷新问题

Flutter 中的with关键字

运行时出现Flutter错误:Error waiting for a debug connection: Bad state: No element

Flutter:Firebase FieldValue.serverTimestamp() 到 DateTime 对象

如何在 Flutter 中找出 URL 的有效性?

多级异步代码中的 Dart 错误

IndexedDB访问速度和效率

Flutter-在屏幕上绘制图形

Flitter中的谷歌 map 没有出现

dart:web_gl: 渲染警告:texture bound to texture unit 0 is not renderable

如何在 Flutter 中匹配密码和确认密码?

如何使用工厂构造函数扩展抽象类?

在 Dart 中调用异步函数而不等待,例如启动一个线程

有没有更好的方法来找出文件或目录是否存在?

可选参数的默认值