The Day My Flutter IDE Went Silent
One day I was working on a Flutter project then I noticed something was off. That helpful suggestion to add const
to my widgets? Gone. The quick fixes that used to automatically optimize my code? Completely vanished.
Something was definitely wrong, and I needed to figure out what happened to one of Flutter's most important performance optimization features.
After trying various fixes, I discovered that the most reliable and permanent solution wasn't restarting servers or reinstalling extensions. It was something much simpler: properly configuring my analysis_options.yaml
file.
Understanding the Real Problem
The issue isn't actually with your IDE or the Dart analysis server most of the time. It's about lint rules configuration. The const suggestions you're missing are powered by specific linter rules that might not be properly enabled in your project.
When Flutter updated its default linting approach and introduced flutter_lints
, many projects were left in a configuration limbo. Some had outdated rules, others had conflicting configurations, and many simply didn't have the const-related rules explicitly enabled.
The analysis_options.yaml Solution
Here's the solution that worked for me and has been rock-solid across multiple projects:
Step 1: Create or Update Your analysis_options.yaml
In your project root (same level as pubspec.yaml
), create or update your analysis_options.yaml
file:
include: package:flutter_lints/flutter.yaml
linter:
rules:
prefer_const_constructors: true
prefer_const_constructors_in_immutables: true
prefer_const_declarations: true
prefer_const_literals_to_create_immutables: true
analyzer:
exclude:
- "build/**"
errors:
prefer_const_constructors: info
prefer_const_constructors_in_immutables: info
prefer_const_declarations: info
prefer_const_literals_to_create_immutables: info
Step 2: Add the Required Dependency
Make sure you have flutter_lints
in your pubspec.yaml
:
dev_dependencies:
flutter_lints: ^3.0.0
# ... your other dev dependencies
Step 3: Apply the Changes
flutter pub get
Then restart your Dart Analysis Server in your IDE.
Breaking Down Each Rule
Let me explain what each of these rules does and why they're crucial:
prefer_const_constructors: true
This is the main rule that suggests adding const
to widget constructors when possible.
// Before: IDE won't suggest const
Container(
child: Text('Hello'),
)
// After: IDE suggests adding const
const Container(
child: Text('Hello'),
)
prefer_const_constructors_in_immutables: true
This rule specifically targets immutable classes and suggests const constructors:
class MyWidget extends StatelessWidget {
// IDE will suggest making this const
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
return const Text('Hello');
}
}
prefer_const_declarations: true
This suggests using const
for variable declarations when the value is compile-time constant:
// IDE suggests const instead of final
const String appTitle = 'My App';
const double padding = 16.0;
prefer_const_literals_to_create_immutables: true
This rule suggests using const constructors for immutable collections:
// IDE suggests const
const List<String> items = ['item1', 'item2'];
const Map<String, int> counts = {'apples': 5, 'oranges': 3};
The Error Level Configuration
The analyzer.errors
section is crucial for controlling how these suggestions appear:
analyzer:
errors:
prefer_const_constructors: info # Shows as blue info
# Could also be: warning, error, or ignore
I recommend using info
level because:
- It provides helpful suggestions without being intrusive
- It doesn't break your build process
- It keeps your code clean with visible but non-blocking hints
Give It a Try
Save your analysis_options.yaml
file, run flutter pub get
, and restart your Dart Analysis Server. You should start seeing those helpful const
suggestions right away!
Now go forth and let your widgets be const
as they should be!
Top comments (0)