Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter says a value must be constant when you pass it a variable

ErrorInvalid constant value.

Short answer

Dart only accepts a const object when every value inside it is known at compile time. A variable, a function parameter or a widget field is only known at run time. Either remove const from the widget or list that holds the variable, or turn the source value into a real const.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the analyzer. It prints the diagnostic name and the exact line, and the name tells you which cause below applies.

    flutter analyze
  2. 2Match what you see, then jump to its fix

    1. 1Arguments of a constant creation must be constant expressions.A const widget or constructor receives a runtime valueFix
    2. 2Const variables must be initialized with a constant value.A const variable is initialised from a non-const variableFix
    3. 3The values in a const list literal must be constants.A const list literal contains a variableFix
    4. 4The default value of an optional parameter must be constant.An optional parameter has a default that is not constFix

What does the error look like?

  • Invalid constant value.
  • Arguments of a constant creation must be constant expressions.
  • Const variables must be initialized with a constant value.
  • The values in a const list literal must be constants.
  • The default value of an optional parameter must be constant.

Cause 1 of 4

A const widget or constructor receives a runtime value

1Check

Look at the line the analyzer points to. A const in front of a widget (or in front of a parent widget) makes every argument inside it need a compile-time value. A parameter, a field or the result of a function call is not one.

// Fails: name is a function parameter, known only at run time
Widget build(String name) => const Text(name);

2Fix

Remove const from the widget that holds the variable. If a parent is also marked const, remove it there too.

Widget build(String name) => Text(name);

3Verify

The analyzer stops reporting the line.

flutter analyze

Confirmed by dart.dev

Did this fix it?

Cause 2 of 4

A const variable is initialised from a non-const variable

1Check

Find the variable on the right-hand side of the const declaration. If it is declared with var or final, Dart cannot treat it as a compile-time value.

var x = 0;
const y = x;

2Fix

Either declare the source value const as well, or, when the value can only be known at run time, declare the second variable final instead of const.

const x = 0;
const y = x;

// or, when x cannot be const
var x = 0;
final y = x;

3Verify

Re-run the analyzer.

flutter analyze

Confirmed by dart.dev

Did this fix it?

Cause 3 of 4

A const list literal contains a variable

1Check

A list is constant when it has the const keyword, or when it sits inside something that is const (for example a const widget). One non-const element is enough to break it.

var x = 2;
var y = const <int>[0, 1, x];

2Fix

Make the element const if it can be one. If it cannot, remove const from the list (and from any const that wraps it).

var x = 2;
var y = <int>[0, 1, x];

3Verify

Re-run the analyzer.

flutter analyze

Confirmed by dart.dev

Did this fix it?

Cause 4 of 4

An optional parameter has a default that is not const

1Check

Look at the default after the equals sign in the function or constructor signature. A top-level var or a call to a function is not allowed there.

var defaultValue = 3;
void f([int value = defaultValue]) {}

2Fix

Make the default a const, or make the parameter nullable and apply the default inside the body.

var defaultValue = 3;
void f([int? value]) {
  value ??= defaultValue;
}

3Verify

Re-run the analyzer.

flutter analyze

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Add const only where the analyzer suggests it; do not put it on a widget that reads a variable.
  • Keep values that change (user data, theme lookups, MediaQuery results) out of const subtrees.
  • Run flutter analyze before committing so the error shows up on one line, not in a failed build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.