Maaz Zindani

Loading the index…

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

Flutter warns that withOpacity is deprecated after upgrading

ErrorwithOpacity is deprecated

Short 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.

  • Applies to Flutter 3.27 and newer, Dart 3.6 and newer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every place the analyzer flags, so you can replace them in one pass.

    flutter analyze | grep -i deprecated
  2. 2Match what you see, then jump to its fix

    1. 1The analyzer points at code such as Colors.black.withOpacity(0.5)Calls to Color.withOpacity()Fix
    2. 2The analyzer flags a read of .opacity on a Color valueReading Color.opacityFix
    3. 3Golden tests or exact colour comparisons change by a tiny amount after the swapCode that depends on the old 8-bit roundingFix

What does the error look like?

  • withOpacity is deprecated
  • Why 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 analyze

Confirmed by docs.flutter.dev

Did this fix it?

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 analyze

Confirmed by docs.flutter.dev

Did this fix it?

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-255

3Verify

Run the affected tests.

flutter test

Confirmed by docs.flutter.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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