Search code examples
android-studioflutterdart

Flutter/Dart find unused files or widgets


I am working on an existing application, it has too many unused files or widgets, I need to find all and cleanup the codes

How can I find unused files/widgets in flutter/dart environment? I am using android studio as my IDE


Solution

  • 2023/08/23, as commented by Tommy Chang and others, The GitHub package has been discontinued and is no longer supported. At the moment, it is only available as a paid service, costing $8 per month. However, as of right now, it still functions but limits your project to older package versions.

    https://github.com/dart-code-checker/dart-code-metrics

    Dart code metrics: https://pub.dev/packages/dart_code_metrics supports finding unused files for Flutter.

    Install:

          flutter pub add --dev dart_code_metrics
    

    run "flutter packages get" or use your IDE to "Pub get".

    run:

         dart run dart_code_metrics:metrics check-unused-files lib
    

    result:

        Unused file: lib/generated_plugin_registrant.dart
        Unused file: lib/ux/Pdf/makepdfdocument.dart
        Unused file: lib/ux/RealEstate/realestatetable.dart
    ...
    Total unused files: 11
    

    How to find unused dart or flutter files (Medium)

    Introduction to dart-code-metrics (Medium)

    Code-metrics can also give insight in:

    • Cyclomatic Complexity
    • Lines of Code
    • Maximum Nesting
    • Number of Methods
    • Number of Parameters
    • Source lines of Code
    • Weight of a Class