function_linter 1.1.0
function_linter: ^1.1.0 copied to clipboard
A specialized tool for Flutter devs, discouraging inline functions to promote clean code. Enhance readability and reusability.
Function Linter Example π #
This example shows you how to use Function Linter in a real project!
π Try It Yourself #
-
Install dependencies:
dart pub get -
Create a Dart file (like
lib/main.dart) with some messy inline functions:import 'package:flutter/material.dart'; class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return ElevatedButton( onPressed: () { // This will get flagged! π¨ print('Button pressed'); showDialog( context: context, builder: (context) => AlertDialog(title: Text('Hello!')), ); }, child: Text('Press me'), ); } } -
Run analysis:
dart analyze -
See the magic! You'll get a helpful warning suggesting to extract the function.
β¨ What You'll See #
The linter will show warnings like:
Warning: Prefer extracting the callback to a separate widget method.
π§ Fix It Like This #
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: _handleButtonPress, // Much cleaner! β¨
child: Text('Press me'),
);
}
void _handleButtonPress() {
print('Button pressed');
showDialog(
context: context,
builder: (context) => AlertDialog(title: Text('Hello!')),
);
}
}
π― Pro Tips #
- The linter only flags complex inline functions (with multiple statements)
- One-liners like
() => print('hello')are totally fine - Builder functions like
ListView.builder()are automatically ignored
Happy coding! π