Function Linter π
Keep your Flutter code clean and organized!
Function Linter is a simple tool that helps you write better Flutter code by catching messy inline functions and suggesting cleaner alternatives. It's like having a code reviewer that never sleeps! π΄
π€ What Problem Does This Solve?
Have you ever written Flutter code that looks like this?
ElevatedButton(
onPressed: () {
// 20 lines of complex logic here...
if (something) {
doThis();
andThat();
maybeThis();
}
// More logic...
},
child: Text('Click me'),
)
This makes your code hard to read, test, and reuse. Function Linter catches these cases and suggests better approaches!
β¨ What You Get
- Cleaner code β Easier to read and understand
- Better testing β Extract functions to test them separately
- Code reuse β Share common functions across widgets
- Consistent style β Your whole team writes organized code
π Quick Start
Step 1: Install
Add this to your pubspec.yaml:
dev_dependencies:
function_linter: ^1.1.0
Step 2: Configure
Add this to your analysis_options.yaml:
analyzer:
plugins:
- custom_lint
Step 3: That's it!
Run dart pub get and the linter will start helping you write better code automatically!
π Examples - Before & After
β This Will Get Flagged
Complex inline functions that make your widgets hard to read:
ElevatedButton(
onPressed: () {
// Multiple lines of complex logic - this gets flagged!
if (user.isLoggedIn) {
analytics.track('button_pressed');
Navigator.push(context, MaterialPageRoute(builder: (context) => ProfilePage()));
} else {
showLoginDialog();
}
},
child: Text('View Profile'),
)
β This Is Much Better
Extract the function for cleaner, testable code:
ElevatedButton(
onPressed: _handleProfileButton, // Clean and simple!
child: Text('View Profile'),
)
void _handleProfileButton() {
if (user.isLoggedIn) {
analytics.track('button_pressed');
Navigator.push(context, MaterialPageRoute(builder: (context) => ProfilePage()));
} else {
showLoginDialog();
}
}
β These Are Fine (Won't Be Flagged)
Simple one-liners:
ElevatedButton(
onPressed: () => Navigator.pop(context), // Short and sweet β
child: Text('Back'),
)
Flutter builder patterns:
ListView.builder(
itemBuilder: (context, index) { // This is a Flutter pattern β
return ListTile(title: Text('Item $index'));
},
)
Function references:
TextField(
onChanged: _handleTextChanged, // Already extracted β
)
π§ How It Works
Function Linter is smart about what it flags:
- Scans your widgets - Looks at all widget constructors
- Finds complex functions - Only flags functions with multiple statements
- Respects Flutter patterns - Ignores builder functions and one-liners
- Shows helpful warnings - Appears right in your IDE
π οΈ Works With Your Favorite Editor
- β Visual Studio Code
- β Android Studio / IntelliJ IDEA
- β Any editor with Dart support
You'll see warnings directly in your code with helpful suggestions!
π€« Need to Skip a Warning?
Sometimes you really need an inline function. No problem:
// ignore: prefer-extracting-callbacks
ElevatedButton(
onPressed: () {
// This won't trigger a warning
},
child: Text('Button'),
)
π‘ Questions?
- Found a bug? Open an issue
- Want to contribute? Pull requests are welcome!
- Need help? Check out our example project
π License
MIT License - see LICENSE for details.
Made with β€οΈ by WebMobTech Solutions
Libraries
- function_linter
- Support for doing something awesome.