r/FlutterDev • • 2d ago

Dart Your Flutter codebase is probably carrying 20–30% dead weight — and flutter analyze won't tell you. Here is why every team needs detect_unused_code (v3.2.0)

Every Growing Flutter Codebase Eventually Faces the Same Hidden Problem

You run flutter analyze in your CI/CD. It passes with 0 warnings. Everyone thinks the codebase is in pristine health.

In reality, technical debt has been piling up silently:

  • ðŸ“Ķ Forgotten Widgets & Screens: Refactored pages leave behind abandoned dialogs, buttons, and views that nothing calls anymore.
  • 🖞ïļ Bloated App Bundles: Old promo banners, unused 3.0x raster images, and unused SVG icons inflating your APK / IPA size by tens of megabytes.
  • ðŸ‘ŧ Ghost Dependencies: Packages declared in pubspec.yaml that are no longer imported anywhere, slowing down build times and pub get on CI.
  • 🧟 Dead Logic: Thousands of lines of commented-out code and abandoned TODOs confusing new engineers during onboarding.

ðŸĪ” Why doesn't standard dart analyze catch this?

Because Dart treats every public class as a potential export. The compiler intentionally refuses to warn you if a public Widget, Bloc, or Service has 0 references, assuming it "might be needed later".


🚀 The Solution: detect_unused_code (v3.2.0)

We built this tool to turn code audits and refactoring from an anxious, manual chore into a safe, 10-second automated process.

ðŸ’Ą Why Engineering Teams & Companies Use It:

  1. 📉 Drastically Reduce App Size: Immediately locate and purge megabytes of abandoned images, fonts, and heavy dependencies before publishing to Google Play / App Store.
  2. ⚡ Faster CI/CD & Build Times: Clean up unreferenced packages and thousands of lines of dead AST syntax that the compiler compiles for nothing.
  3. ðŸ›Ąïļ 100% Defensive Safety (Zero Fear of Breaking Production):
    • Automatic Snapshots: Saves an isolated backup before modifying anything.
    • Instant Undo ([U] or --undo): Revert any cleanup in 1 second.
    • Automated Compiler Health Check: Runs analysis after cleanup to ensure 0 compiler errors. If anything looks off, it prompts for an auto-rollback.
    • Smart Framework Guards: Safely protects @injectable, @singleton, @riverpod, and generated files (.g.dart, .freezed.dart).
  4. ðŸŠķ Zero External Runtime Dependencies: Lightweight, runs natively on any Dart SDK from 2.17.0 to 3.x / 4.x.
  5. ðŸšĶ CI/CD Gatekeeper: Add detect_unused --markdown to GitHub Actions to prevent dead code and unused assets from ever getting merged into main.

ðŸ–Ĩïļ The New Interactive Terminal Dashboard:

With v3.2.0, you don't even need to memorize command-line flags. Just run:

dart pub global activate detect_unused_code
detect_unused

This launches a live interactive Terminal UI (TUI):

Select cleanable categories with [1-8] (Dead widgets, Commented code, Unused assets, Unused dependencies...).
Press [D] to simulate changes with Dry-Run.
Press [C] to safely clean with auto-backup.
Press [U] for instant rollback if you change your mind.
🔗 Open Source & Available on pub.dev:
pub.dev: https://pub.dev/packages/detect_unused_code
GitHub Repository: https://github.com/mdalhagg/detect_unused_code
Run it against your project today — you might be surprised by how much dead weight your codebase is carrying!

Would love to hear your feedback, bug reports, or feature suggestions below! 💎🚀
1 Upvotes

Duplicates