How to Fix all ’Prefer const with constant constructors’ Warning in Flutter/Dart

In this example, we are going to show you how to fix all error message warnings "Prefer const with constant constructors" in Flutter/Dart. You can ignore an individual message, but it's annoying to fix it all at the same time.

Prefer const with constant constructors, dart (prefer_const_constructors) (x, y)

To ignore this message in a specific file only, add the following line anywhere in your dart script file.

// ignore_for_file: prefer_const_constructors

We have added this line after importing, you can add anywhere you want in the file where you want to ignore 'const' error warning.

To Fix this 'const' error in all dart files, open analysis_options.yaml file located at the project root, and add the line below linter:rules:

linter:
  rules:
    prefer_const_constructors : false

After adding this line, open the terminal add to hit the following command to apply these changes.

dart fix --apply

Output on Terminal:

PS C:\flutter_apps\testapp> dart fix --apply
Computing fixes in testapp...
Applying fixes...

lib\main.dart
  prefer_const_literals_to_create_immutables • 1 fix

1 fix made in 1 file.

After this, your all warning message will disappear. 

In this way, you can ignore the 'Prefer const with constant constructors' warning in specific as well as in all files in Flutter/Dart.

No any Comments on this Article


Please Wait...