Flutter/Dart developers, rejoice! If you've ever felt like your Dart code could use a sprinkle of performance magic, this extension is for you. It automatically adds the const keyword to constructors and static methods where it’s missing.
Benefits:
Performance: const objects are compile-time constants, which means they’re optimized at build time. Your app will thank you for the speed boost.
Cleaner Code: Fewer manual changes mean a cleaner codebase and less chance of missing crucial optimizations.
Less Stress: No more worrying about those irritatating warnings — we’ve got you covered!
🛠️ Features
Automatic Optimization: Adds const where applicable based on diagnostics.
Manual Trigger: Use the command palette to manually optimize your Dart code.
On Save: Automatically optimize Dart files upon saving.
🚀 How to Use
1. Install the Extension
Open VS Code.
Go to Extensions (Ctrl+Shift+X on Windows or Cmd+Shift+X on Mac).
Search for Flutter Const Optimizer.
Click Install.
2. Optimize Your Code
Manual Optimization
Open the Command Palette (Ctrl+Shift+P on Windows or Cmd+Shift+P on Mac).
Type Flutter Const Optimizer: Optimize Code.
Press Enter.
Voila! Your code will be optimized.
Automatic Optimization on Save
Simply save your Dart file.
The extension will automatically check for optimization opportunities and apply them.
3. Verify the Changes
The extension will show a notification for each optimization applied. If you have any issues or need to review changes, you’ll see relevant messages in the info panel.
🧩 How It Works
Diagnostics Check: The extension scans for Dart diagnostics suggesting const optimizations.
Text Analysis: It evaluates where const can be added.
Code Update: Applies the const keyword and saves your file automatically.
🎨 Example
Before:
class MyClass {
MyClass();
}
void myMethod() {
MyClass();
}