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

Commit d740adb

Browse files
committed
fix: check didChangeDependencies for avoid-unnecessary-setstate
1 parent 4cecc46 commit d740adb

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* fix: check `didChangeDependencies` for [`avoid-unnecessary-setstate`](https://dcm.dev/docs/individuals/rules/flutter/avoid-unnecessary-setstate).
56
* feat: add `allow-nullable` config option for [`avoid-returning-widgets`](https://dcm.dev/docs/individuals/rules/common/avoid-returning-widgets).
67
* fix: support `assert(mounted)` for [`use-setstate-synchronously`](https://dcm.dev/docs/individuals/rules/flutter/use-setstate-synchronously).
78
* fix: correctly support dartdoc tags for [`format-comment`](https://dcm.dev/docs/individuals/rules/common/format-comment).

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_unnecessary_setstate/visitor.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
part of 'avoid_unnecessary_setstate_rule.dart';
22

33
class _Visitor extends RecursiveAstVisitor<void> {
4-
static const _checkedMethods = ['initState', 'didUpdateWidget', 'build'];
4+
static const _checkedMethods = [
5+
'initState',
6+
'didUpdateWidget',
7+
'didChangeDependencies',
8+
'build',
9+
];
510

611
final _setStateInvocations = <MethodInvocation>[];
712
final _classMethodsInvocations = <MethodInvocation>[];

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_unnecessary_setstate/avoid_unnecessary_setstate_rule_test.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void main() {
2727

2828
RuleTestHelper.verifyIssues(
2929
issues: issues,
30-
startLines: [16, 22, 35, 57, 63, 27, 78],
31-
startColumns: [5, 7, 5, 5, 7, 5, 5],
30+
startLines: [16, 22, 35, 45, 68, 74, 27, 48, 89],
31+
startColumns: [5, 7, 5, 5, 5, 7, 5, 5, 5],
3232
locationTexts: [
3333
'setState(() {\n'
3434
' myString = "Hello";\n'
@@ -42,18 +42,24 @@ void main() {
4242
'setState(() {\n'
4343
' myString = "Hello";\n'
4444
' })',
45+
'setState(() {\n'
46+
' myString = "Hello";\n'
47+
' })',
4548
'setState(() {\n'
4649
' myString = "Hello";\n'
4750
' })',
4851
'myMethod()',
4952
'myMethod()',
53+
'myMethod()',
5054
],
5155
messages: [
5256
'Avoid calling unnecessary setState. Consider changing the state directly.',
5357
'Avoid calling unnecessary setState. Consider changing the state directly.',
5458
'Avoid calling unnecessary setState. Consider changing the state directly.',
5559
'Avoid calling unnecessary setState. Consider changing the state directly.',
5660
'Avoid calling unnecessary setState. Consider changing the state directly.',
61+
'Avoid calling unnecessary setState. Consider changing the state directly.',
62+
'Avoid calling a sync method with setState.',
5763
'Avoid calling a sync method with setState.',
5864
'Avoid calling a sync method with setState.',
5965
],

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_unnecessary_setstate/examples/example.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ class _MyWidgetState extends State<MyWidget> {
3737
});
3838
}
3939

40+
@override
41+
void didChangeDependencies() {
42+
super.didChangeDependencies();
43+
44+
// LINT
45+
setState(() {
46+
myString = "Hello";
47+
});
48+
myMethod(); // LINT
49+
}
50+
4051
void myMethod() {
4152
setState(() {
4253
myString = "Hello";

0 commit comments

Comments
 (0)