Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

fix: check didChangeDependencies for avoid-unnecessary-setstate #1188

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* fix: check `didChangeDependencies` for [`avoid-unnecessary-setstate`](https://dcm.dev/docs/individuals/rules/flutter/avoid-unnecessary-setstate).
* fix: add new config option for [`no-equal-arguments`](https://dcm.dev/docs/individuals/rules/common/no-equal-arguments).
* feat: add `allow-nullable` config option for [`avoid-returning-widgets`](https://dcm.dev/docs/individuals/rules/common/avoid-returning-widgets).
* fix: support `assert(mounted)` for [`use-setstate-synchronously`](https://dcm.dev/docs/individuals/rules/flutter/use-setstate-synchronously).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
part of 'avoid_unnecessary_setstate_rule.dart';

class _Visitor extends RecursiveAstVisitor<void> {
static const _checkedMethods = ['initState', 'didUpdateWidget', 'build'];
static const _checkedMethods = [
'initState',
'didUpdateWidget',
'didChangeDependencies',
'build',
];

final _setStateInvocations = <MethodInvocation>[];
final _classMethodsInvocations = <MethodInvocation>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void main() {

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [16, 22, 35, 57, 63, 27, 78],
startColumns: [5, 7, 5, 5, 7, 5, 5],
startLines: [16, 22, 35, 45, 68, 74, 27, 48, 89],
startColumns: [5, 7, 5, 5, 5, 7, 5, 5, 5],
locationTexts: [
'setState(() {\n'
' myString = "Hello";\n'
Expand All @@ -42,18 +42,24 @@ void main() {
'setState(() {\n'
' myString = "Hello";\n'
' })',
'setState(() {\n'
' myString = "Hello";\n'
' })',
'setState(() {\n'
' myString = "Hello";\n'
' })',
'myMethod()',
'myMethod()',
'myMethod()',
],
messages: [
'Avoid calling unnecessary setState. Consider changing the state directly.',
'Avoid calling unnecessary setState. Consider changing the state directly.',
'Avoid calling unnecessary setState. Consider changing the state directly.',
'Avoid calling unnecessary setState. Consider changing the state directly.',
'Avoid calling unnecessary setState. Consider changing the state directly.',
'Avoid calling unnecessary setState. Consider changing the state directly.',
'Avoid calling a sync method with setState.',
'Avoid calling a sync method with setState.',
'Avoid calling a sync method with setState.',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ class _MyWidgetState extends State<MyWidget> {
});
}

@override
void didChangeDependencies() {
super.didChangeDependencies();

// LINT
setState(() {
myString = "Hello";
});
myMethod(); // LINT
}

void myMethod() {
setState(() {
myString = "Hello";
Expand Down