Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart/Linter (Flutter) no longer detects const constructors?

I am just getting back into coding with Flutter, and it used to be the case that my IDE would tell me when it was better to use const constructors (I use VS Code). I haven't seen that at all in my recent projects, and when going to linter and trying to add the rule prefer_const_constructors, it says it doesn't exist.

Anybody know what might be going on? enter image description here

like image 672
BrainStormer Avatar asked Oct 20 '25 19:10

BrainStormer


2 Answers

In your analysis_options.yaml, you need to have rules: between linter: and prefer_const_constructors: true. Indentation should be as follows:

linter:
  rules:
    prefer_const_constructors: true
like image 111
mmcdon20 Avatar answered Oct 24 '25 21:10

mmcdon20


Const rules were removed

The reason you can no longer see the const lint warnings in your recent projects is because the const lint rules below were recently removed from the default lints in the flutter_lints package:

  • prefer_const_constructors,
  • prefer_const_declarations, and
  • prefer_const_literals_to_create_immutables

Add the rules manually

To continue using them, include them in your analysis_options.yaml file.

include: package:flutter_lints/flutter.yaml

linter:
  rules:
    prefer_const_constructors: true  
    prefer_const_declarations: true 
    prefer_const_literals_to_create_immutables: true  

See the discussion here.

like image 40
Victor Eronmosele Avatar answered Oct 24 '25 20:10

Victor Eronmosele