Function Linter πŸ”

pub package License: MIT

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:

  1. Scans your widgets - Looks at all widget constructors
  2. Finds complex functions - Only flags functions with multiple statements
  3. Respects Flutter patterns - Ignores builder functions and one-liners
  4. 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?

πŸ“„ License

MIT License - see LICENSE for details.

Made with ❀️ by WebMobTech Solutions

Libraries

function_linter
Support for doing something awesome.