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

Commit e8a40d9

Browse files
committed
chore: review fixes
1 parent 48b53eb commit e8a40d9

File tree

4 files changed

+114
-29
lines changed

4 files changed

+114
-29
lines changed

doc/rules/avoid-unnecessary-setstate.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class _MyWidgetState extends State<MyWidget> {
4545
});
4646
}
4747
48-
myMethod(); // LINT
48+
myStateUpdateMethod(); // LINT
4949
}
5050
5151
@override
@@ -56,7 +56,7 @@ class _MyWidgetState extends State<MyWidget> {
5656
});
5757
}
5858
59-
void myMethod() {
59+
void myStateUpdateMethod() {
6060
setState(() {
6161
myString = "Hello";
6262
});
@@ -76,10 +76,10 @@ class _MyWidgetState extends State<MyWidget> {
7676
});
7777
}
7878
79-
myMethod(); // LINT
79+
myStateUpdateMethod(); // LINT
8080
8181
return ElevatedButton(
82-
onPressed: () => myMethod(),
82+
onPressed: () => myStateUpdateMethod(),
8383
onLongPress: () {
8484
setState(() {
8585
myString = data;
@@ -108,16 +108,18 @@ class _MyWidgetState extends State<MyWidget> {
108108
void initState() {
109109
super.initState();
110110
111+
myString = "Hello";
112+
111113
classWithMethod.myMethod();
112114
myAsyncMethod();
113115
}
114116
115117
@override
116118
void didUpdateWidget(MyWidget oldWidget) {
117-
...
119+
myString = "Hello";
118120
}
119121
120-
void myMethod() {
122+
void myStateUpdateMethod() {
121123
setState(() {
122124
myString = "Hello";
123125
});
@@ -136,7 +138,7 @@ class _MyWidgetState extends State<MyWidget> {
136138
myAsyncMethod();
137139
138140
return ElevatedButton(
139-
onPressed: () => myMethod(),
141+
onPressed: () => myStateUpdateMethod(),
140142
onLongPress: () {
141143
setState(() {
142144
myString = data;

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ part 'visitor.dart';
1616
class AvoidUnnecessarySetStateRule extends Rule {
1717
static const String ruleId = 'avoid-unnecessary-setstate';
1818

19-
static const _warningMessage = 'Avoid calling unnecessary setState.';
19+
static const _warningMessage =
20+
'Avoid calling unnecessary setState. Consider changing the state directly.';
2021
static const _methodWarningMessage = 'Avoid calling a method with setState.';
2122

2223
AvoidUnnecessarySetStateRule([Map<String, Object> config = const {}])
2324
: super(
2425
id: ruleId,
2526
documentation: const RuleDocumentation(
2627
name: 'Avoid returning widgets',
27-
brief: 'Warns ',
28+
brief:
29+
'Warns when `setState` is called inside `initState`, `didUpdateWidget` or `build` methods and when it is called from a `sync` method that is called inside those methods.',
2830
),
2931
severity: readSeverity(config, Severity.warning),
3032
excludes: readExcludes(config),
@@ -37,26 +39,22 @@ class AvoidUnnecessarySetStateRule extends Rule {
3739
source.unit.visitChildren(_visitor);
3840

3941
return [
40-
..._visitor.setStateInvocations
41-
.map((invocation) => createIssue(
42-
rule: this,
43-
location: nodeLocation(
44-
node: invocation,
45-
source: source,
46-
),
47-
message: _warningMessage,
48-
))
49-
.toList(growable: false),
50-
..._visitor.classMethodsInvocations
51-
.map((invocation) => createIssue(
52-
rule: this,
53-
location: nodeLocation(
54-
node: invocation,
55-
source: source,
56-
),
57-
message: _methodWarningMessage,
58-
))
59-
.toList(growable: false)
42+
..._visitor.setStateInvocations.map((invocation) => createIssue(
43+
rule: this,
44+
location: nodeLocation(
45+
node: invocation,
46+
source: source,
47+
),
48+
message: _warningMessage,
49+
)),
50+
..._visitor.classMethodsInvocations.map((invocation) => createIssue(
51+
rule: this,
52+
location: nodeLocation(
53+
node: invocation,
54+
source: source,
55+
),
56+
message: _methodWarningMessage,
57+
))
6058
];
6159
}
6260
}

test/analyzers/lint_analyzer/rules/rules_list/avoid_unnecessary_setstate/avoid_unnecessary_setstate_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import '../../../../../helpers/rule_test_helper.dart';
88
// ignore_for_file: no_adjacent_strings_in_list
99

1010
const _examplePath = 'avoid_unnecessary_setstate/examples/example.dart';
11+
const _stateSubclassExamplePath =
12+
'avoid_unnecessary_setstate/examples/state_subclass_example.dart';
1113

1214
void main() {
1315
group('AvoidUnnecessarySetStateRule', () {
@@ -62,5 +64,13 @@ void main() {
6264
],
6365
);
6466
});
67+
68+
test('reports no issues with State subclass', () async {
69+
final unit =
70+
await RuleTestHelper.resolveFromFile(_stateSubclassExamplePath);
71+
final issues = AvoidUnnecessarySetStateRule().check(unit);
72+
73+
RuleTestHelper.verifyNoIssues(issues);
74+
});
6575
});
6676
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class MyWidget extends StatefulWidget {
2+
@override
3+
_MyWidgetState createState() => _MyWidgetState();
4+
}
5+
6+
class _MyWidgetState extends BaseState<MyWidget> {
7+
String myString = '';
8+
9+
@override
10+
void initState() {
11+
super.initState();
12+
13+
classWithMethod.myMethod();
14+
myAsyncMethod();
15+
}
16+
17+
void myMethod() {
18+
setState(() {
19+
myString = "Hello";
20+
});
21+
}
22+
23+
Future<void> myAsyncMethod() async {
24+
final data = await service.fetchData();
25+
26+
setState(() {
27+
myString = data;
28+
});
29+
}
30+
31+
@override
32+
Widget build(BuildContext context) {
33+
final widget = ElevatedButton(
34+
onPressed: () => myMethod(),
35+
onLongPress: () {
36+
setState(() {
37+
myString = data;
38+
});
39+
},
40+
child: Text('PRESS'),
41+
);
42+
43+
myAsyncMethod();
44+
45+
return ElevatedButton(
46+
onPressed: () => myMethod(),
47+
onLongPress: () {
48+
setState(() {
49+
myString = data;
50+
});
51+
},
52+
child: Text('PRESS'),
53+
);
54+
}
55+
}
56+
57+
class ElevatedButton {
58+
final Function onPressed;
59+
final Function onLongPress;
60+
final dynamic child;
61+
62+
const ElevatedButton(
63+
this.onPressed,
64+
this.onLongPress,
65+
this.child,
66+
);
67+
}
68+
69+
class SomeClassWithMethod {
70+
void myMethod() {}
71+
}
72+
73+
class State<T> {}
74+
75+
class BaseState<T> extends State<T> {}

0 commit comments

Comments
 (0)