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.
Android Studio has significantly less Flutter-related plugins than VS Code. Searching for “Flutter” on the Android Studio marketplace only yields 61 results. However, all of the critical tools, like packages to help with popular state management frameworks or converting JSON into Dart code are readily available.
Many people use a Java-based IDE for Flutter development, but this isn't the only option. IntelliJ IDEA, Visual Studio Code, and Android Studio are all great choices.
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:
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With