Skip to main content

avoid-undisposed-instances

added in: 1.13.0

Warns when an instance that has a dispose method is not assigned to a variable.

Not disposing such instances may lead to memory leaks and should be avoided.

⚙️ Config

Set ignored-instances (default is none) to ignore instances that are intended to be undisposed.

dart_code_metrics:
...
rules:
...
- avoid-undisposed-instances:
ignored-instances:
- SomeClass

Example

❌ Bad:

TextSpan(
...
recognizer: LongPressGestureRecognizer() // LINT
..onLongPress = _handlePress,
),

✅ Good:

class _SomeState extends State<Some> {
late LongPressGestureRecognizer _longPressRecognizer;


void initState() {
super.initState();
_longPressRecognizer = LongPressGestureRecognizer()
..onLongPress = _handlePress;
}


void dispose() {
_longPressRecognizer.dispose();
super.dispose();
}


Widget build(BuildContext context) {
...
TextSpan(
...
recognizer: _longPressRecognizer,
),
...
}
}