You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Injectable section and handler lifecycle warnings to docs
- Add "Want Less Boilerplate? Try Injectable!" section to get_it getting_started
- Move "Naming Your GetIt Instance" section earlier in the doc flow
- Add handler lifecycle warning to watch_it observing_commands, handlers, and debugging docs
- Create handler_lifecycle_example.dart code sample
- Apply all changes to both English and Spanish documentation
- `final di = GetIt.instance;`(dependency injection)
81
+
- `GetIt.instance`or `GetIt.I` (use directly without variable)
82
+
83
+
<strong>Recommendation:</strong> Use `getIt` or `di` - both are clear and widely recognized in the Flutter community.
84
+
85
+
::: tip Using with `watch_it`
86
+
If you're using the [`watch_it`](https://pub.dev/packages/watch_it) package, you already have a global `di` instance available - no need to create your own. Just import `watch_it` and use `di` directly.
87
+
:::
88
+
89
+
::: tip Cross-Package Usage
90
+
`GetIt.instance`returns the same singleton across all packages in your project. Create your global variable once in your main app and import it elsewhere.
91
+
:::
92
+
68
93
::: warning Isolate Safety
69
94
GetIt instances are not thread-safe and cannot be shared across isolates. Each isolate will get its own GetIt instance. This means objects registered in one isolate can't be accessed from another isolate.
70
95
:::
@@ -73,7 +98,7 @@ GetIt instances are not thread-safe and cannot be shared across isolates. Each i
73
98
74
99
## When to Use Which Registration Type
75
100
76
-
get_it offers three main registration types:
101
+
`get_it` offers three main registration types:
77
102
78
103
| Registration Type | When Created | Lifetime | Use When |
@@ -161,6 +186,93 @@ See [Where should I put my get_it setup code?](/documentation/get_it/faq#where-s
161
186
162
187
---
163
188
189
+
## Want Less Boilerplate? Try Injectable!
190
+
191
+
**Injectable** is a code generation package that automates your `get_it` setup. If you find yourself writing lots of registration code, injectable might be for you.
Run build_runner and injectable generates all the registration code for you.
221
+
222
+
### Quick Setup
223
+
224
+
1. Add dependencies:
225
+
```yaml
226
+
dependencies:
227
+
get_it: ^8.3.0
228
+
injectable: ^2.3.2
229
+
230
+
dev_dependencies:
231
+
injectable_generator: ^2.6.1
232
+
build_runner: ^2.4.0
233
+
```
234
+
235
+
2. Annotate your main configuration:
236
+
```dart
237
+
@InjectableInit()
238
+
void configureDependencies() => getIt.init();
239
+
```
240
+
241
+
3. Run code generation:
242
+
```bash
243
+
flutter pub run build_runner build
244
+
```
245
+
246
+
### When to Use Injectable
247
+
248
+
**Use injectable if:**
249
+
- You have many services to register
250
+
- You want automatic dependency resolution
251
+
- You prefer annotations over manual setup
252
+
- You're comfortable with code generation
253
+
254
+
**Stick with plain `get_it` if:**
255
+
- You prefer explicit, visible registration
256
+
- You want to avoid build_runner in your workflow
257
+
- You have a small number of services
258
+
- You want full control over registration order and logic
259
+
260
+
Both approaches use the same `GetIt` instance and have identical runtime performance!
261
+
262
+
### Learn More
263
+
264
+
Check out [injectable on pub.dev](https://pub.dev/packages/injectable) for full documentation and advanced features like:
265
+
- Environment-specific registrations (dev/prod)
266
+
- Pre-resolved dependencies
267
+
- Module registration
268
+
- Custom annotations
269
+
270
+
::: tip Injectable Support
271
+
Injectable is a separate package maintained by a different author. If you encounter issues with injectable, please file them on the [injectable GitHub repository](https://github.com/Milad-Akarie/injectable/issues).
272
+
:::
273
+
274
+
---
275
+
164
276
## Next Steps
165
277
166
278
Now that you understand the basics, explore these topics:
@@ -228,27 +340,3 @@ get_it implements the Service Locator pattern - it decouples interface (abstract
228
340
For deeper understanding, read Martin Fowler's classic article: [Inversion of Control Containers and the Dependency Injection pattern](https://martinfowler.com/articles/injection.html)
229
341
230
342
</details>
231
-
232
-
---
233
-
234
-
## Naming Your GetIt Instance
235
-
236
-
The standard pattern is to create a global variable:
- `final di = GetIt.instance;`(dependency injection)
245
-
- `GetIt.instance`or `GetIt.I` (use directly without variable)
246
-
247
-
<strong>Recommendation:</strong> Use `getIt` or `di` - both are clear and widely recognized in the Flutter community.
248
-
249
-
::: tip Using with `watch_it`
250
-
If you're using the [`watch_it`](https://pub.dev/packages/watch_it) package, you already have a global `di` instance available - no need to create your own. Just import `watch_it` and use `di` directly.
251
-
:::
252
-
253
-
::: tip Cross-Package Usage
254
-
`GetIt.instance`returns the same singleton across all packages in your project. Create your global variable once in your main app and import it elsewhere.
If the widget containing the handler is destroyed and rebuilt while the command is running, the handler will be re-registered and may miss state changes.
142
+
143
+
**Example:** A button inside a widget that rebuilds on hover:
Copy file name to clipboardExpand all lines: docs/documentation/watch_it/observing_commands.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,6 +67,16 @@ While `watch` is for rebuilding UI, use `registerHandler` for side effects like
67
67
- Log error to crash reporting
68
68
- Retry logic
69
69
70
+
::: warning Handler Lifecycle
71
+
If you rely on your handler reacting to all state changes, ensure the widget where the handler is registered isn't destroyed and rebuilt during command execution.
72
+
73
+
**Common pitfall:** A button that registers a handler and calls a command, but the parent widget rebuilds on an `onHover` event - this destroys and recreates the button (and its handler), causing missed state changes.
74
+
75
+
**Solution:** Move the handler to a parent widget that will live for the entire duration of the command execution.
76
+
77
+
**Note:** This doesn't apply to `watch` functions - their results are only used in the same build function, so widget rebuilds don't cause issues.
78
+
:::
79
+
70
80
## Watching Command Results
71
81
72
82
The `results` property provides a `CommandResult` object containing all command state in one place:
0 commit comments