-
Notifications
You must be signed in to change notification settings - Fork 13
Feat: adding new lint: avoid_missing_dispose #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c600b39
fcc23ae
2ccd6a0
d59f090
9c6320c
554d33b
3a8f573
b1f39f3
fd57f03
c89f6dc
eb50a43
0f2d6ba
62c8af1
fd08a41
f4309dc
9f54966
34a0e67
538a026
e7c94a8
3dcd430
6783459
1030a5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -469,6 +469,85 @@ class MyWidget extends StatelessWidget { | |
|
|
||
| None | ||
|
|
||
| ### `missing_cleanup` | ||
|
|
||
| **DO** cleanup of resources that require `dispose()`, `close()` or `cancel()` in StatefulWidget `State` classes. | ||
|
|
||
| Resources such as controllers, stream controllers or focus nodes must be cleanup in the `dispose()` method to prevent memory leaks. | ||
|
|
||
| **BAD:**` | ||
|
|
||
| ```dart | ||
| class MyWidgetState extends State<MyWidget> { | ||
| late final TextEditingController controller = TextEditingController(); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return TextField(controller: controller); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **BAD:** | ||
|
|
||
| ```dart | ||
| class MyWidgetState extends State<MyWidget> { | ||
| @override | ||
| Widget build(BuildContext context) { | ||
| return TextField(controller: TextEditingController()); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **GOOD:** | ||
|
|
||
| ```dart | ||
| class MyWidgetState extends State<MyWidget> { | ||
| late final TextEditingController controller; | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| controller = TextEditingController(); | ||
| } | ||
|
Comment on lines
+508
to
+512
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this GOOD example confusing: is it good because of the dispose in the |
||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return TextField(controller: controller); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| controller.dispose(); | ||
| super.dispose(); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### Configuration | ||
|
|
||
| ```yaml | ||
| custom_lint: | ||
| rules: | ||
| - missing_cleanup: | ||
| ignored_types: | ||
| - ignore: AnimationController | ||
| from_package: flutter | ||
| cleanup_methods: | ||
| close: true | ||
| dispose: true | ||
| cancel: true | ||
|
|
||
| ``` | ||
|
|
||
| - `ignored_types` - an optional YamlList - skips dispose checks for specified types. This allows disabling the lint rule for classes where dispose method checks are not needed. | ||
| - `ignore` - A required String - name of the instance to ignore | ||
| - `from_package` - A required String - name of the source package | ||
| - `cleanup_methods` - an optional YamlMap - controls which disposal methods the lint rule should recognize and check for. By default, the rule looks for `dispose`, `close`, and `cancel` methods. You can selectively enable or disable checking for each of these methods. | ||
|
Comment on lines
+543
to
+546
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this configuration? What is the usecase of disabling some of them globally (rather than putting a type in the |
||
| - `dispose` - A boolean (default: true) - whether to check for `dispose()` method calls | ||
| - `close` - A boolean (default: true) - whether to check for `close()` method calls | ||
| - `cancel` - A boolean (default: true) - whether to check for `cancel()` method calls | ||
|
|
||
| ## Assists | ||
|
|
||
| Assists are IDE refactorings not related to a particular issue. They can be triggered by placing your cursor over a relevant piece of code and opening the code actions dialog. For instance, in VSCode this is done with <kbd>ctrl</kbd>+<kbd>.</kbd> or <kbd>⌘</kbd>+<kbd>.</kbd>. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.