sdk
sdk copied to clipboard
unused_element could be based on a tree-shaking, "reachability" analysis.
Private subclasses of StatefulWidgets are not reported as unused as I would have expected.
import 'package:flutter/material.dart';
void main() {
print('The private widget class is unused.');
}
// 🔥 BAD: No unused_element reported on next line.
class _UnusedPrivateStatefulWidget extends StatefulWidget {
const _UnusedPrivateStatefulWidget({Key? key}) : super(key: key);
@override
State<_UnusedPrivateStatefulWidget> createState() => _UnusedPrivateStatefulWidgetState();
}
class _UnusedPrivateStatefulWidgetState extends State<_UnusedPrivateStatefulWidget> {
@override
Widget build(BuildContext context) {
return Container();
}
}
// 👍 GOOD: unused_element reported on next line, as expected.
class _UnusedPrivateStatelessWidget extends StatelessWidget {
const _UnusedPrivateStatelessWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container();
}
}
// 👍 GOOD: unused_element reported on next line, as expected.
int _unusedPrivateInt = 0;
If I had to speculate, my guess is that this is unreported because the StatefulWidget subclass is used by the State subclass and the State subclass is used by the StatefulWidget subclass. However, both are really unused by the app and the lint should remind developers to removed the unused widget.