29

Because rapid change in app requirement from client, I have nearly 200 dart files, and many of them are unused. Is there any way for me to find unused classes except resorting to manually search one by one? In Android Studio, I can find java or kotlin classes which are unused by using "inspect code" feature. But I can't find any tools for the same purpose for dart / flutter.

Thank you for your help in advance.

Dika
  • 2,213
  • 4
  • 33
  • 49

4 Answers4

32

Dart code metrics https://pub.dev/packages/dart_code_metrics supports finding unused files for flutter. As commented by in 2023, unfortunately, it is not free anymore https://dcm.dev/pricing/. To purchase a license, see their website.

install:

      flutter pub add --dev dart_code_metrics

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

run:

     flutter pub 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

Howo 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
1

Unused classes in the project are usually searched by the option Global unused analysis , as mentioned in one of the previous answers on this topic https://stackoverflow.com/a/38244028/10498374

I think the dart files are not supported yet.

A temporary solution, you can delete the file that you do not want and if it is used within the project, Android Studio will tell you that it is used

farouk osama
  • 2,374
  • 2
  • 12
  • 30
  • 1
    Or you can Ctrl+Click it and see if some class is referencing it. If not, then delete it safely. Anyways, Dart Analysis tab will alert you and also the project won't compile. – Joaquin Iurchuk Aug 09 '20 at 02:12
1

In VS code all unused classes in a file are marked grey or underlined, and when you hover on them a popup will appear telling you that the class has not been used anywhere within your the current file. you can then delete all of such classes.

alternatively, for each class you suspect is unused, you can make a global search, by pressing on the loop icon on the top left side of vs and input the name of the class as your search key, from there you can see if it is used or not

Mich25educ
  • 295
  • 1
  • 15
0

I'm not sure about unused classes per se. I know you can clean up unused imported files at least.

In Android Studio there is an Optimize Imports function that you can call on a particular file. It removes any unused imports. It appears you can also configure the setting on on commits in the settings. Check out Version Control -> Commit Dialog for Optimize Imports.

In VSCode's Problems tab, you can see unused variables, I'm not 100% sure it applies to classes though. But your editor may also notify you via its syntax highlighting of a class that is unused as well.

Jack
  • 1,125
  • 1
  • 13
  • 29