Flutter says a value must be constant when you pass it a variable
Invalid 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.
Fix it fast
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 analyze2Match what you see, then jump to its fix
- 1
Arguments of a constant creation must be constant expressions.A const widget or constructor receives a runtime valueFix - 2
Const variables must be initialized with a constant value.A const variable is initialised from a non-const variableFix - 3
The values in a const list literal must be constants.A const list literal contains a variableFix - 4
The default value of an optional parameter must be constant.An optional parameter has a default that is not constFix
- 1
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 analyzeConfirmed by dart.dev
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 analyzeConfirmed by dart.dev
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 analyzeConfirmed by dart.dev
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 analyzeConfirmed by dart.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix