call method
- void action(), {
- bool guard()?,
- int? take,
- void onUncaughtError(
- Object error,
- StackTrace stackTrace
Defines an explicit manifest Effect attached to the scoped targets.
-
Skips initial evaluation (0 runs upon construction/mount).
-
Executes
actionin an untracked environment when any of the scoped targets mutate. -
If
guardis provided and returnsfalse, execution is skipped andtaketickets are preserved. -
If
takeis provided, executes at mosttaketimes and then automatically deactivates (Early Teardown). -
If
onUncaughtErroris provided, it is invoked when an uncaught exception is thrown inactionorguard. -
action: The untracked callback to invoke upon mutation. -
guard: Optional boolean predicate gating execution and preserving tickets whenfalse. -
take: Optional limit on execution count before automatic deactivation. -
onUncaughtError: Optional error recovery handler.
Requires:
- If
takeis provided, it must be greater than or equal to0.
Ensures:
- Returns a manifest Effect bound to the scoped targets.
Warning (Anti-Pattern Prevention):
- Do NOT mutate scoped targets synchronously inside
actionwithout an exit condition.
BAD:
// [Anti-Pattern] Mutating target inside its own effect action
[counter.bloom].effect(() {
counter.set(untracked(counter.bloom) + 1); // Triggers infinite invalidation loop!
});
GOOD:
// [Recommended] Perform external side-effects
[counter.bloom].effect(() {
analyticsService.log(untracked(counter.bloom));
});
Examples
final effect = [user.bloom, role.bloom].effect(() {
refreshPermissions(untracked(user.bloom), untracked(role.bloom));
});
Implementation
@pragma('vm:prefer-inline')
Effect call(
void Function() action, {
bool Function()? guard,
int? take,
void Function(Object error, StackTrace stackTrace)? onUncaughtError,
}) =>
Effect(
_targets,
action,
guard: guard,
take: take,
onUncaughtError: onUncaughtError,
);