Flutter warns that withOpacity is deprecated after upgrading
withOpacity is deprecatedShort answer
Since Flutter 3.27 a Color keeps its alpha as a floating-point value, and withOpacity() squeezed it into an 8-bit integer, which loses precision. Replace color.withOpacity(x) with color.withValues(alpha: x), and color.opacity with color.a. The old calls still compile, so the warning does not break a build.
Fix it fast
1Find out which cause you have
List every place the analyzer flags, so you can replace them in one pass.
flutter analyze | grep -i deprecated2Match what you see, then jump to its fix
- 1
The analyzer points at code such as Colors.black.withOpacity(0.5)Calls to Color.withOpacity()Fix - 2
The analyzer flags a read of .opacity on a Color valueReading Color.opacityFix - 3
Golden tests or exact colour comparisons change by a tiny amount after the swapCode that depends on the old 8-bit roundingFix
- 1
What does the error look like?
withOpacity is deprecatedWhy is withOpacity deprecated in Flutter 3.27.0
Cause 1 of 3
Calls to Color.withOpacity()
1Check
Find each call in the project. Flutter's migration guide says withOpacity can lose data because the alpha channel used to be an 8-bit integer.
grep -rn "withOpacity(" lib/2Fix
Use withValues and pass the alpha as a double between 0.0 and 1.0. The value is not rounded to 8 bits any more.
// before
final c = Colors.black.withOpacity(0.5);
// after
final c = Colors.black.withValues(alpha: 0.5);3Verify
Run the analyzer again. The count of deprecation lines for withOpacity should drop to zero.
flutter analyzeConfirmed by docs.flutter.dev
Cause 2 of 3
Reading Color.opacity
1Check
Search for reads of the old getter on Color values.
grep -rnE "\.opacity\b" lib/2Fix
Read the alpha channel with the a getter. It returns a double from 0.0 to 1.0.
// before
final x = color.opacity;
// after
final x = color.a;3Verify
The analyzer no longer reports the getter.
flutter analyzeConfirmed by docs.flutter.dev
Cause 3 of 3
Code that depends on the old 8-bit rounding
1Check
withOpacity(0.5) produced an alpha of about 0.502, while withValues(alpha: 0.5) gives exactly 0.5. Tests that compare raw channel values see that difference.
// prints 0.5019607843137255
print(Colors.black.withOpacity(0.5).a);
// prints 0.5
print(Colors.black.withValues(alpha: 0.5).a);2Fix
Update the expected values. If you really need the old quantised alpha, use the 8-bit .alpha getter and withAlpha(), which Flutter keeps for that case.
final c = Colors.black.withAlpha(128); // 0-255
final a = c.alpha; // 0-2553Verify
Run the affected tests.
flutter testConfirmed by docs.flutter.dev
How do I stop it coming back?
- Keep flutter analyze in CI so a new deprecation shows up in a pull request, not after an upgrade.
- Read the breaking-changes page when you move to a new stable Flutter release.
- Use withValues in new code from the start.
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