The argument type 'MaterialColor' can't be assigned to 'MaterialStateProperty<Color>'
The argument type 'MaterialColor' can't be assigned to the parameter type 'MaterialStateProperty<Color?>?'Short answer
ButtonStyle and similar properties take a per-state value, not a plain color. Wrap one color for every state in WidgetStatePropertyAll, return different colors with WidgetStateProperty.resolveWith, or skip ButtonStyle and use the button's styleFrom shortcut. MaterialStateProperty is the old name of the same class.
Fix it fast
1Find out which cause you have
Open the line the analyzer points at. Note which property gets the color (backgroundColor, foregroundColor, fillColor): that property wants a state property.
flutter analyze2Match what you see, then jump to its fix
- 1
The error sits on backgroundColor: or foregroundColor: inside style: ButtonStyle(...)A raw color is passed into ButtonStyleFix - 2
You only need a background and a text colorYou want one simple color, not per-state logicFix - 3
The same error on fillColor: of a Checkbox, or on a property you want to change when disabledDifferent colors per state on a checkbox or buttonFix - 4
Code or an example still says MaterialStateProperty or MaterialState.pressedOld MaterialState names in codeFix
- 1
What does the error look like?
The argument type 'MaterialColor' can't be assigned to the parameter type 'MaterialStateProperty<Color?>?'
Cause 1 of 4
A raw color is passed into ButtonStyle
1Check
In ButtonStyle these properties resolve a color per state (pressed, hovered, focused, disabled). A Colors.red value is not one.
ElevatedButton(
style: ButtonStyle(backgroundColor: Colors.red), // error
onPressed: () {},
child: const Text('Save'),
)2Fix
For one color in every state, wrap it in WidgetStatePropertyAll.
ElevatedButton(
style: ButtonStyle(
backgroundColor: WidgetStatePropertyAll<Color>(Colors.red),
),
onPressed: () {},
child: const Text('Save'),
)3Verify
The analyzer error is gone and the button shows the color.
flutter analyzeConfirmed by api.flutter.dev
Cause 2 of 4
You want one simple color, not per-state logic
1Check
styleFrom takes plain colors and builds the state properties for you.
grep -rn "ButtonStyle(" lib/2Fix
Use the styleFrom shortcut of the button class.
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
onPressed: () {},
child: const Text('Save'),
)3Verify
Run the app: the button shows both colors.
flutter runConfirmed by api.flutter.dev
Cause 3 of 4
Different colors per state on a checkbox or button
1Check
Checkbox.fillColor takes a WidgetStateProperty<Color?>, resolved for the selected, hovered, focused and disabled states.
grep -rn "fillColor:" lib/2Fix
Return a color from the set of states with WidgetStateProperty.resolveWith. Returning null falls back to the default.
Checkbox(
value: checked,
onChanged: (v) => setState(() => checked = v!),
fillColor: WidgetStateProperty.resolveWith<Color?>(
(Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return Colors.grey;
}
return Colors.orange;
},
),
)3Verify
Toggle enabled and disabled and compare the colors.
flutter runConfirmed by api.flutter.dev
Cause 4 of 4
Old MaterialState names in code
1Check
Since Flutter 3.22 these were moved and renamed to WidgetState. It is the same behavior with new names.
grep -rn "MaterialState" lib/2Fix
Replace MaterialState with WidgetState, MaterialStateProperty with WidgetStateProperty, and MaterialStatePropertyAll with WidgetStatePropertyAll.
grep -rln "MaterialStateProperty" lib/ | xargs sed -i '' 's/MaterialStateProperty/WidgetStateProperty/g'3Verify
Search again: no MaterialState left apart from the two input border classes.
grep -rn "MaterialState" lib/Confirmed by docs.flutter.dev
How do I stop it coming back?
- Prefer styleFrom for simple colors.
- Use the Flutter docs for your installed version, not an old tutorial.
- Read the breaking-changes page after each Flutter upgrade.
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