账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    [dart] Arguments of a constant creation must be constant express
    27
    0

    用flutter开发app时,想定义一些常用的样式

    代码如下:

    import 'package:flutter/material.dart';
    class AppStyle {
      static Color colorRed = const Color(0xffe04f5f);
      static Color colorWhite = const Color(0xffffffff);
      static Color colorGreen = const Color(0xff1abc9c);
    }

    现在,想定义一个列表标题的样式,添加一行

      static TextStyle listRowTitle = const TextStyle(fontSize: 20.0, color: colorGreen);

    如果向上面这样写,那么colorGreen这里就会出问题,错误信息是

    [dart] Invalid constant value.
    [dart] Arguments of a constant creation must be constant expressions.
    Color colorGreen

    如果,把colorGreen换成Color(0xff1abc9c)就没有问题!

    static TextStyle listRowTitle = const TextStyle(fontSize: 20.0, color: Color(0xff1abc9c));

    自己google了半天,也没找到答案,哪位大神可以给我解释解释啊?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 逆疯飞扬 普通会员 1楼

      In Dart, arguments of a constant creation must be constant expressions. A constant creation is a type of expression that is used to create a constant value. These expressions are typically used to define constants, such as a number or a string, that will not change during the execution of a program. To ensure that the arguments of a constant creation are constant expressions, you can use the const keyword followed by the expression that you want to create a constant value for. For example: const num = 5;

      In this example, the const keyword is used to create a constant variable named num. The expression 5 is used as the constant value for this variable. This means that the value of num will never change during the execution of the program. It's also important to note that when creating a constant value in Dart, you can use the = operator to assign the value of a constant expression to a variable. For example: const num = 5; num = 10;

      In this example, the value of num is updated to 10 because the = operator is used to assign the value of 5 to num. I hope this helps! Let me know if you have any other questions.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部