Skip to content

Commit 49ab244

Browse files
committed
WIP: Start extracting remaining inline code to external files (1/3)
Extracted code from: - your_first_watch_functions.md: Try It Yourself section (4 snippets) - more_watch_functions.md: All comparison snippets (6 snippets) Created: - try_it_yourself_example.dart (4 regions) - watch_comparison_snippets.dart (6 regions) All examples compile successfully. Still TODO: watching_streams_and_futures, handlers, getting_started, watching_widgets, watch_ordering_rules, lifecycle, observing_commands, best_practices, debugging_tracing, advanced_integration
1 parent b620682 commit 49ab244

File tree

4 files changed

+115
-56
lines changed

4 files changed

+115
-56
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:watch_it/watch_it.dart' hide di;
3+
import '_shared/stubs.dart';
4+
5+
// #region manager
6+
class MyManager {
7+
final message = ValueNotifier<String>('Hello');
8+
}
9+
// #endregion manager
10+
11+
// #region register
12+
void setupMyManager() {
13+
di.registerSingleton<MyManager>(MyManager());
14+
}
15+
// #endregion register
16+
17+
// #region watch
18+
class MyWidget extends WatchingWidget {
19+
@override
20+
Widget build(BuildContext context) {
21+
final message = watchValue((MyManager m) => m.message);
22+
return Text(message);
23+
}
24+
}
25+
// #endregion watch
26+
27+
// #region change
28+
void changeMessage() {
29+
di<MyManager>().message.value = 'World!'; // Widget rebuilds!
30+
}
31+
// #endregion change
32+
33+
void main() {
34+
setupMyManager();
35+
runApp(MaterialApp(home: MyWidget()));
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ignore_for_file: unused_local_variable, unreachable_from_main
2+
import 'package:flutter/material.dart';
3+
import 'package:watch_it/watch_it.dart' hide di;
4+
import '_shared/stubs.dart';
5+
6+
// #region property_value_difference
7+
void propertyValueDifference(BuildContext context) {
8+
// Rebuilds on EVERY SettingsModel change
9+
final settings = watchIt<SettingsModel>();
10+
final darkMode1 = settings.darkMode;
11+
12+
// Rebuilds ONLY when darkMode changes
13+
final darkMode2 = watchPropertyValue((SettingsModel m) => m.darkMode);
14+
}
15+
// #endregion property_value_difference
16+
17+
// #region quick_comparison
18+
void quickComparison(BuildContext context) {
19+
// 1. watchValue - Most common
20+
final todos = watchValue((TodoManager m) => m.todos);
21+
22+
// 2. watchIt - When manager IS a Listenable
23+
final manager = watchIt<CounterModel>();
24+
25+
// 3. watch - Local or direct Listenable
26+
final counter = createOnce(() => ValueNotifier(0));
27+
final count = watch(counter).value;
28+
29+
// 4. watchPropertyValue - Selective updates
30+
final darkMode = watchPropertyValue((SettingsModel m) => m.darkMode);
31+
}
32+
// #endregion quick_comparison
33+
34+
// #region watchValue_usage
35+
void watchValueUsage(BuildContext context) {
36+
final data = watchValue((DataManager m) => m.data);
37+
}
38+
// #endregion watchValue_usage
39+
40+
// #region watchIt_usage
41+
void watchItUsage(BuildContext context) {
42+
final manager = watchIt<CounterModel>();
43+
// Can call methods on manager
44+
}
45+
// #endregion watchIt_usage
46+
47+
// #region watch_usage
48+
void watchUsage(BuildContext context) {
49+
final controller = createOnce(() => TextEditingController());
50+
final text = watch(controller).value.text;
51+
}
52+
// #endregion watch_usage
53+
54+
// #region watchPropertyValue_usage
55+
void watchPropertyValueUsage(BuildContext context) {
56+
// Only rebuild when THIS specific property changes
57+
final darkMode = watchPropertyValue((SettingsModel m) => m.darkMode);
58+
}
59+
// #endregion watchPropertyValue_usage
60+
61+
void main() {}

docs/documentation/watch_it/more_watch_functions.md

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,56 +31,30 @@ Only rebuild when a specific property changes:
3131
<<< @/../code_samples/lib/watch_it/watch_property_value_selective_example.dart#example
3232

3333
**The difference:**
34-
```dart
35-
// Rebuilds on EVERY SettingsModel change
36-
final settings = watchIt<SettingsModel>();
37-
final darkMode = settings.darkMode;
3834

39-
// Rebuilds ONLY when darkMode changes
40-
final darkMode = watchPropertyValue((SettingsModel m) => m.darkMode);
41-
```
35+
<<< @/../code_samples/lib/watch_it/watch_comparison_snippets.dart#property_value_difference
4236

4337
## Quick Comparison
4438

45-
```dart
46-
// 1. watchValue - Most common
47-
final todos = watchValue((TodoManager m) => m.todos);
48-
49-
// 2. watchIt - When manager IS a Listenable
50-
final manager = watchIt<TodoManager>();
51-
52-
// 3. watch - Local or direct Listenable
53-
final counter = createOnce(() => ValueNotifier(0));
54-
final count = watch(counter).value;
55-
56-
// 4. watchPropertyValue - Selective updates
57-
final darkMode = watchPropertyValue((Settings m) => m.darkMode);
58-
```
39+
<<< @/../code_samples/lib/watch_it/watch_comparison_snippets.dart#quick_comparison
5940

6041
## Choosing the Right Function
6142

6243
**Start with `watchValue()`** - it's the most common:
63-
```dart
64-
final data = watchValue((Manager m) => m.data);
65-
```
44+
45+
<<< @/../code_samples/lib/watch_it/watch_comparison_snippets.dart#watchValue_usage
6646

6747
**Use `watchIt()` when you need the whole object:**
68-
```dart
69-
final manager = watchIt<TodoManager>();
70-
manager.addTodo('New todo');
71-
```
48+
49+
<<< @/../code_samples/lib/watch_it/watch_comparison_snippets.dart#watchIt_usage
7250

7351
**Use `watch()` for local Listenables:**
74-
```dart
75-
final controller = createOnce(() => TextEditingController());
76-
final text = watch(controller).value.text;
77-
```
52+
53+
<<< @/../code_samples/lib/watch_it/watch_comparison_snippets.dart#watch_usage
7854

7955
**Use `watchPropertyValue()` for optimization:**
80-
```dart
81-
// Only rebuild when THIS specific property changes
82-
final darkMode = watchPropertyValue((Settings m) => m.darkMode);
83-
```
56+
57+
<<< @/../code_samples/lib/watch_it/watch_comparison_snippets.dart#watchPropertyValue_usage
8458

8559
## Practical Example
8660

docs/documentation/watch_it/your_first_watch_functions.md

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,20 @@ Add a todo? Widget rebuilds automatically. No `setState`, no `StreamBuilder`.
4949
## Try It Yourself
5050

5151
1. Create a `ValueNotifier` in your manager:
52-
```dart
53-
class MyManager {
54-
final message = ValueNotifier<String>('Hello');
55-
}
56-
```
52+
53+
<<< @/../code_samples/lib/watch_it/try_it_yourself_example.dart#manager
5754

5855
2. Register it:
59-
```dart
60-
di.registerSingleton<MyManager>(MyManager());
61-
```
56+
57+
<<< @/../code_samples/lib/watch_it/try_it_yourself_example.dart#register
6258

6359
3. Watch it:
64-
```dart
65-
class MyWidget extends WatchingWidget {
66-
@override
67-
Widget build(BuildContext context) {
68-
final message = watchValue((MyManager m) => m.message);
69-
return Text(message);
70-
}
71-
}
72-
```
60+
61+
<<< @/../code_samples/lib/watch_it/try_it_yourself_example.dart#watch
7362

7463
4. Change it and watch the magic:
75-
```dart
76-
di<MyManager>().message.value = 'World!'; // Widget rebuilds!
77-
```
64+
65+
<<< @/../code_samples/lib/watch_it/try_it_yourself_example.dart#change
7866

7967
## Key Takeaways
8068

0 commit comments

Comments
 (0)