dart中的“常量创建的参数必须是常量表达式”是什么? [英] What is the `Arguments of a constant creation must be constant expressions` in dart?

查看:225
本文介绍了dart中的“常量创建的参数必须是常量表达式”是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么Dart编译器在代码中向我显示错误。这到底是什么意思?谢谢。

I don't know why the dart compiler shows me an error in my code. What is that actually means? Thanks.

来源:

          const SliverAppBar(
        pinned: true,
        expandedHeight: 300.0, // TODO: check out later
        flexibleSpace: FlexibleSpaceBar(
            title: new Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Text('_SliverAppBar'),
                Text('subtitle'),
              ],
            ),
            background: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('Info'),
              ],
            )),

推荐答案

我已评论了导致该问题的原始答案。但这是为什么:

I have commented the original answer that lead to that problem. But here is why:

正如@aziza所指出的,您已将 SliverAppBar 实例化为 const 关键字。因此,所有属性都应使用 const 实例化。

As @aziza pointed, you have instantiated your SliverAppBar with the const keyword. Therefore, all properties should be instantiated with const.

在您的情况下,只需更改 new Column const Column 可以解决该问题,但是dart 2可以推断出它将如何实例化该类。只需省略 new const 关键字,然后让dart完成工作即可。

In your case, just changing from new Column to const Column would solve the problem, but, dart 2 can infer how it will instantiate the class. Just omit the new and const keywords, and let dart do the work.

这篇关于dart中的“常量创建的参数必须是常量表达式”是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆