138
var textSize = 10.0;
// or
double textSize = 10.0;

into Text Widget of Flutter

child: const Text('Calculate Client Fees',
                   style: TextStyle(fontSize: textSize),)

Here it is giving error

Invalid Constant Value

Do we have to compulsory use const value? Why can not we use var or double?

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
Gaurang Goda
  • 3,394
  • 4
  • 20
  • 29

9 Answers9

302

You are declaring your Text widget as a const, which requires all of its children to be const as well. If you want to fix this, you should not use a const Text widget in this case as you want to pass a non-const variable.

The reason for this is that Flutter uses the const keyword as an idicator for a widget to never rebuild as it will get evaluated at compile time and only once. Hence, every part of it has to be constant as well.

double textSize = 10.04;
// ...
child: Text('Calculate Client Fees', style: TextStyle(fontSize: textSize))

Read more about it in this article.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • 8
    Quite literally I have spent about a day on this, and have explored literally EVERY other possibility to get this to work. Turns out I simply had the parent widget set to a constant. You sir are a lifesaver. – Saphiric Oct 13 '21 at 15:03
44

Don't use the const keyword if you are not using fixed values. For example, in the case of Text, if its string is constant like Text("something here") then we should use the const but if the string of Text is dynamic then don't use const before Text widget. const Text("something") and Text(anyVariabale) The same is the case for all widgets.

Muhammad Umair Saqib
  • 1,287
  • 1
  • 9
  • 20
  • 3
    A little explanation and code would definitely help. – Mahmood Sanjrani Sep 20 '21 at 11:28
  • 2
    basically you need to remove the `const` before the Text widget, since the value is not yet set (or might change) at compile time – Yonatan Dec 15 '21 at 13:48
  • 1
    A little explanation and code IN THE ANSWER ITSELF would definitely help. ;) I got it, though! But it's a good general practice. Thousands of people will read this answer over the years...... ;D – Karolina Hagegård May 29 '22 at 13:32
11

As @creativecreatorormaybenot said you are using const Text() which is why you have to have a const value there. You can either use

const double textSize = 10.0;

or

const textSize = 10.0;

Just like this case.

Padding(
  padding: const EdgeInsets.all(value), // this value has to be a `const` because our padding: is marked const
  child: Text("HI there"),
);


Padding(
  padding: EdgeInsets.all(10), // any double value
  child: Text("HI there"),
);
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
11

In dart when you pass something as a parameter in a const constructor, the compiler makes sure that the value set as default is not changed during the execution of the code.

Hence, the Invalid constant value warning.

To resolve this issue you should remove the const keyword from the in front of the Text.

Delicia Fernandes
  • 582
  • 12
  • 19
7
child: const Text('Calculate Client Fees',
                   style: TextStyle(fontSize: textSize),)

should be:

child: Text('Calculate Client Fees',
                   style: TextStyle(fontSize: textSize),)
Jessy Ndaya
  • 185
  • 3
  • 4
6

You should remove the keywords const

When it is used, it put the widget into the cache.

And you can't wait for a value into your widget and put the widget again as a constance. When you want to do that, you should not put your widget constant.

So do that:

double textSize = 10.0;
child: Text('Calculate Client Fees', style: TextStyle(fontSize: textSize),)
Boris Kamtou
  • 454
  • 4
  • 5
3

Sometimes this can be tricky because the Text widget might not be the one which is const (where the error is displayed), but maybe its parent widget is const or the parent of that parent widget, etc. In that case it could be surprising and not immediate to spot the solution. For example:

      const PrefLabel(
        title: Text(
          preferenceOptionTitle,
          style: Get.textTheme.headline5!,
          maxLines: 3,
        ),
        subtitle: Text(preferenceOptionDescription),
      ),

In this case the Text is not marked with const because the PrefLabel is already const. The corrected one which passes linting: const is moved to the subtitle

      PrefLabel(
        title: Text(
          preferenceOptionTitle,
          style: Get.textTheme.headline5!,
          maxLines: 3,
        ),
        subtitle: const Text(preferenceOptionDescription),
      ),
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
1

if you want to use the var or double textSize = 10.0; then the text widget must not be a const. remove the const before the Text()

  child: Text('Calculate Client Fees', style: TextStyle(fontSize: textSize),)
culjo
  • 387
  • 4
  • 9
-1

The simplest solution is to Just remove the const keyword. replace with:

child: const Text('Calculate Client Fees',
               style: TextStyle(fontSize: textSize),)
Saad Mansoor
  • 199
  • 7