Skip to content

Commit b604674

Browse files
committed
WIP: Extract remaining inline code from getting_started.md (2/3)
Extracted code from: - getting_started.md: Main counter example + mixin example Created: - mixin_simple_example.dart Reused: - counter_simple_example.dart (already had all necessary regions) All examples compile successfully. Still TODO: watching_streams_and_futures, handlers, watching_widgets, watch_ordering_rules, lifecycle, observing_commands, best_practices, debugging_tracing, advanced_integration (100+ inline code blocks)
1 parent 49ab244 commit b604674

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:watch_it/watch_it.dart' hide di;
3+
import '_shared/stubs.dart';
4+
5+
// #region example
6+
class MyWidget extends StatelessWidget with WatchItMixin {
7+
const MyWidget({super.key});
8+
9+
@override
10+
Widget build(BuildContext context) {
11+
final data = watchValue((DataManager m) => m.data);
12+
return Text('$data');
13+
}
14+
}
15+
// #endregion example
16+
17+
void main() {
18+
setupDependencyInjection();
19+
runApp(MaterialApp(home: MyWidget()));
20+
}

docs/documentation/watch_it/getting_started.md

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,7 @@ dependencies:
3535
3636
Here's a simple counter that rebuilds automatically when the count changes:
3737
38-
```dart
39-
// 1. Create a manager with reactive state
40-
class CounterManager {
41-
final count = ValueNotifier<int>(0);
42-
43-
void increment() => count.value++;
44-
}
45-
46-
// 2. Register it in get_it
47-
void main() {
48-
di.registerSingleton<CounterManager>(CounterManager());
49-
runApp(MyApp());
50-
}
51-
52-
// 3. Watch it in your widget
53-
class CounterWidget extends WatchingWidget {
54-
@override
55-
Widget build(BuildContext context) {
56-
// This one line makes it reactive!
57-
final count = watchValue((CounterManager m) => m.count);
58-
59-
return Scaffold(
60-
body: Center(
61-
child: Text('Count: $count', style: TextStyle(fontSize: 48)),
62-
),
63-
floatingActionButton: FloatingActionButton(
64-
onPressed: () => di<CounterManager>().increment(),
65-
child: Icon(Icons.add),
66-
),
67-
);
68-
}
69-
}
70-
```
38+
<<< @/../code_samples/lib/watch_it/counter_simple_example.dart#example
7139
7240
That's it! When you press the button, the widget automatically rebuilds with the new count.
7341
@@ -83,16 +51,7 @@ The widget automatically subscribes to changes when it builds and cleans up when
8351

8452
Already have an app? Just add a mixin to your existing widgets:
8553

86-
```dart
87-
class MyWidget extends StatelessWidget with WatchItMixin {
88-
const MyWidget({super.key});
89-
90-
Widget build(BuildContext context) {
91-
final data = watchValue((Manager m) => m.data);
92-
return Text('$data');
93-
}
94-
}
95-
```
54+
<<< @/../code_samples/lib/watch_it/mixin_simple_example.dart#example
9655

9756
No need to change your widget hierarchy - just add `with WatchItMixin` and start using watch functions.
9857

0 commit comments

Comments
 (0)