Skip to content

Commit 1dbb045

Browse files
committed
Remove incorrect 'watches after early returns' violation
Early returns are ALWAYS safe - watches after them simply don't execute and don't participate in ordering. The problem is only conditional watches on the SAME execution path followed by other watches. Changes: - Removed 'Watch After Early Returns' violation section - Updated 'Early Returns Are Always Safe' section with correct explanation - Removed early returns from DON'T list and troubleshooting - Added to DO list: 'Use early returns freely - they're always safe' - Updated tip box to clarify early returns create separate execution paths
1 parent e50a774 commit 1dbb045

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

docs/documentation/watch_it/watch_ordering_rules.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,20 @@ The most common mistake is putting watch calls inside conditional statements:
4545

4646
<<< @/../code_samples/lib/watch_it/watch_ordering_patterns.dart#watch_inside_loops_wrong
4747

48-
### ❌ Watches AFTER Early Returns
49-
50-
<<< @/../code_samples/lib/watch_it/watch_ordering_patterns.dart#watch_after_early_return_wrong
51-
52-
**Why this breaks:** Watches AFTER early returns may or may not be called, disrupting the order.
53-
54-
**Important:** Early returns themselves are not the problem - watches that come AFTER them are the problem!
55-
5648
### ❌ Watch in Callbacks
5749

5850
<<< @/../code_samples/lib/watch_it/watch_ordering_patterns.dart#watch_in_callbacks_wrong
5951

6052
## Safe Exceptions to the Rule
6153

6254
::: tip Understanding When Conditionals Are Safe
63-
The ordering rule only matters when **subsequent watches** exist. If a watch is conditional but **no watches follow it**, there's no order disruption!
55+
The ordering rule only matters when watches **may or may not be called on the SAME execution path**.
56+
57+
- **Conditional watches at the end** - safe because no watches follow
58+
- **Early returns** - always safe because they create separate execution paths
6459
:::
6560

61+
6662
### ✅ Conditional Watches at the END
6763

6864
Conditional watches are **perfectly safe** when they're the last watches in your build:
@@ -91,23 +87,24 @@ class MyWidget extends WatchingWidget {
9187
- Conditional watch is LAST - no subsequent watches to disrupt
9288
- On rebuild: same order maintained
9389

94-
### ✅ Early Returns After All Watches
90+
### ✅ Early Returns Are Always Safe
9591

96-
Early returns are **safe** when all watches have already been called:
92+
Early returns don't affect watch ordering because watches after them are never called:
9793

9894
```dart
9995
class MyWidget extends WatchingWidget {
10096
@override
10197
Widget build(BuildContext context) {
102-
// Call ALL watches first
103-
final data = watchValue((DataManager m) => m.data);
10498
final isLoading = watchValue((DataManager m) => m.isLoading);
10599
106-
// ✅ Early return AFTER watches - safe!
100+
// ✅ Early return - completely safe!
107101
if (isLoading) {
108102
return CircularProgressIndicator();
109103
}
110104
105+
// This watch only executes when NOT loading
106+
final data = watchValue((DataManager m) => m.data);
107+
111108
if (data.isEmpty) {
112109
return Text('No data');
113110
}
@@ -118,11 +115,11 @@ class MyWidget extends WatchingWidget {
118115
```
119116

120117
**Why this is safe:**
121-
- All watches execute before any early return
122-
- Order is complete and consistent
123-
- Return statements don't affect watch order
118+
- Watches after early returns simply never execute
119+
- They don't participate in the ordering mechanism
120+
- No order disruption possible
124121

125-
**Key principle:** The danger is watches that may or may not be called FOLLOWED by other watches. If your conditional/early return is at the end, there are no subsequent watches to worry about.
122+
**Key principle:** The danger is watches that **may or may not be called** on the SAME build path FOLLOWED by other watches. Early returns create separate execution paths, so watches after them are not part of the ordering for that path.
126123

127124
## Safe Conditional Patterns
128125

@@ -160,7 +157,6 @@ class MyWidget extends WatchingWidget {
160157
**Solution:**
161158
- Check for watches inside `if` statements **followed by other watches**
162159
- Check for watches inside loops
163-
- Check for watches after early returns **when other watches exist after the return**
164160
- Move conditional watches to the END if you need them
165161

166162
### Unexpected Data
@@ -174,17 +170,17 @@ class MyWidget extends WatchingWidget {
174170
## Best Practices Checklist
175171

176172
**DO:**
177-
- Call all watches at the top of `build()`
178-
- Use unconditional watch calls when possible
173+
- Call all watches at the top of `build()` when possible
174+
- Use unconditional watch calls for watches that need to execute on all paths
179175
- Store values in variables, use variables conditionally
180176
- Watch the full list, iterate over values
181-
- Use conditional watches or early returns **at the end** (after all other watches)
177+
- Use conditional watches at the end (after all other watches)
178+
- Use early returns freely - they're always safe
182179

183180
**DON'T:**
184181
- Put watches in `if` statements **when followed by other watches**
185182
- Put watches in loops
186183
- Put watches in callbacks
187-
- Have watches after early returns
188184

189185
## Advanced: Why This Happens
190186

0 commit comments

Comments
 (0)