This repository was archived by the owner on Jul 16, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
website/docs/rules/flutter Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ that the widget has been unmounted needs to be checked before calling [`setState
11
11
Consider storing Futures directly in your state and use [ ` FutureBuilder ` ] ( https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html )
12
12
to unwrap them.
13
13
14
- If not possible, you can also check for [ ` mounted ` ] to only update state when the widget is still mounted. However, an effective fix usually
14
+ If this not possible, you can also check for [ ` mounted ` ] to only update state when the widget is still mounted. However, an effective fix usually
15
15
does not make use of [ ` mounted ` ] , but rather revolves around refactoring your states.
16
16
17
17
:::info
@@ -68,6 +68,29 @@ class _MyWidgetState extends State<MyWidget> {
68
68
69
69
**✅ Good:**
70
70
71
+ ```dart
72
+ class _MyWidgetState extends State<MyWidget> {
73
+ String message ;
74
+
75
+ @override
76
+ Widget build (BuildContext context ) {
77
+ return Button(
78
+ onPressed : () async {
79
+ String fromServer = await fetch (... );
80
+ if (mounted ) {
81
+ setState (() {
82
+ message = fromServer ;
83
+ });
84
+ }
85
+ },
86
+ child : Text (message ),
87
+ );
88
+ }
89
+ }
90
+ ```
91
+
92
+ **✅ Good:**
93
+
71
94
```dart
72
95
class _MyWidgetState extends State<MyWidget> {
73
96
Future < String > message ;
You can’t perform that action at this time.
0 commit comments