Introduction
Workers in GetX are a powerful way to react to state changes. Beyond the basics (ever, once, debounce, interval), you can create complex reactive flows by combining workers, conditionally triggering side effects, and using everAll and onceAll. This guide covers advanced worker patterns to handle intricate business logic with minimal code.
- Recap: Basic Workers
ever– Executes on every change of a reactive variable.once– Executes only on the first change.debounce– Waits for a pause before executing (ideal for search inputs).interval– Executes at a fixed rate while changes occur.everAll– Executes when any of a list of reactive variables changes.onceAll– Executes once when any of a list of reactive variables changes.
- Chaining Workers
You can chain workers to create multi‑step reactive flows. For example, after a debounced search, call another worker to log the result.
- Conditional Triggers
Sometimes you only want to react when a variable reaches a certain value. You can wrap the worker callback with a condition, or use a custom Rx with a filter.
- Combining Multiple Rx with everAll
everAll listens to a list of reactive variables and triggers when any of them change. It provides the list of new values. This is perfect for validating forms or combining multiple state pieces.
- onceAll – One‑Time Reaction
onceAll executes only once when any of the observed variables change. It's useful for onboarding flows or one‑time setups triggered by user interaction.
- Custom Worker Logic: Debounce with Immediate First Call
Sometimes you need a debounce that also triggers immediately on the first change. You can implement a custom worker or combine once and debounce.
- Throttling with interval
interval executes at a fixed interval while the variable changes. This can be used to throttle high‑frequency events like scroll or mouse movements.
- Worker Cleanup & Disposal
Workers are automatically disposed when the controller is disposed. However, if you need to stop a worker manually (e.g., conditionally), you can use a flag inside the worker callback. There's no direct API to remove a worker, but you can set a condition that prevents execution.
- Asynchronous Workers
Workers can perform async operations, but you need to be careful about overlapping calls. Use a flag to prevent multiple executions or cancel previous requests.
- Performance Considerations
- Avoid heavy computations inside worker callbacks – Move heavy work to separate isolates or use
compute. - Use
debouncefor frequent changes – Prevents excessive executions. - Unsubscribe from expensive workers when not needed – Use a conditional flag or dispose controller early.
- Prefer
everover polling – It’s event‑driven and more efficient.
Best Practices
- Keep workers focused – One worker per side effect.
- Use
everAllfor dependent state – Reduces duplicate logic. - Combine
debouncewithoncefor immediate + delayed actions – Improves UX. - Always handle errors in async workers – Prevent uncaught exceptions.
- Test workers – Use
Get.resetand simulate state changes to verify behavior.
Common Mistakes
- ❌ Creating workers that cause infinite loops – Modifying the same Rx variable inside its own worker. ✅ Avoid modifying the observed variable; use a flag or different variable.
- ❌ Forgetting to cancel previous async calls – Results from old requests may overwrite new ones. ✅ Use a cancel token or ignore stale responses.
- ❌ Using
everwhendebounceis more appropriate – Unnecessary CPU usage. ✅ Choose the right worker type. - ❌ Not handling null in workers – The value may be null if the Rx is cleared. ✅ Check for null.
FAQ
- Q: Can I use workers with GetBuilder?
A: Workers are independent of UI; they react to Rx variables. They work regardless of whether the UI usesObxorGetBuilder. - Q: How do I stop a worker?
A: Workers are tied to the controller's lifecycle. To stop it early, you can use a conditional flag inside the callback, or delete the controller withGet.delete. - Q: Can workers listen to multiple variables with different debounce times?
A: Not directly; you would need separate workers or combine them with a custom Rx that aggregates changes. - Q: What's the difference between
everAllandeverwith a list?
A:everAlltriggers when any of the observed variables change, and you get all their values. You can also useeveron a computed getter that depends on them, but that runs on every access. - Q: Can I use workers inside a service (GetxService)?
A: Yes, services have the same lifecycle and can use workers.
Conclusion
Advanced worker patterns allow you to build sophisticated reactive flows with minimal code. By combining workers, handling async operations, and leveraging everAll and onceAll, you can orchestrate complex side effects cleanly. Remember to test your workers and consider performance for heavy operations.