dart Arguments of a constant creation must be constant expressions

3

When developing app with flutter, i want to define some common styles.

The code is as follows:

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);
}

Now, i want to define a new style.

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

If you write to the above, then colorGreen will have a problem here. The wrong message is

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

If you change colorGreen to Color (0xff1abc9c), there is no problem!

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

Ask me to teach meļ¼Œplease!

dart
flutter
asked on Stack Overflow Oct 26, 2018 by sunmoon

2 Answers

1

Since, colors are defined in a class you have to do something like below:

AppStyle.colorGreen

Udate:

Ohh, I see, you are using cont TextStyle. So, you can remove const or add const for your AppStyle.

I simply removed the const from TextStyle:

TextStyle(fontSize: 20.0, color: AppStyle.colorGreen)

Understand how const works.

answered on Stack Overflow Oct 26, 2018 by Blasanka • edited Oct 26, 2018 by Blasanka
-1

The problem is you are declaring a variable value (colorGreen) to a property (color) of a constant widget. Constant widgets cannot vary, change.

Just remove the modifier const from your widget

static TextStyle listRowTitle = const TextStyle(fontSize: 20.0, color: colorGreen);
answered on Stack Overflow Jan 24, 2020 by egidiocs

User contributions licensed under CC BY-SA 3.0