Side effect failing #26
-
I have this: extension _UseConvenience on SideEffectRegistrar {
SideEffectRegistrar get use => this;
}
extension ResourceSideEffects on SideEffectRegistrar {
CountdownSideEffectController asCountdown(
int initialSecondsCount, {
bool autoStart = false,
}) =>
use.register((api) {
final (state, setState) = use.state(initialSecondsCount);
Timer startTimer() =>
Timer.periodic(const Duration(seconds: 1), (timer) {
setState(state - 1);
if (state == 0) {
timer.cancel();
}
api.rebuild();
});
final (timer, setTimer) =
use.state<Timer?>(autoStart ? startTimer() : null);
bool isActive() => timer?.isActive ?? false;
return (
value: '$state seconds',
isActive: isActive(),
pause: () {
if (!isActive()) {
return false;
}
timer!.cancel();
return true;
},
resume: () {
if (isActive()) {
return false;
}
setTimer(startTimer());
return true;
},
reset: () => setState(initialSecondsCount),
);
});
}
typedef CountdownSideEffectController = ({
String value,
bool isActive,
bool Function() pause,
bool Function() resume,
void Function() reset,
}); It's always failing inside the This is the error:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
I'm gonna try to do it without a custom Side Effect. |
Beta Was this translation helpful? Give feedback.
-
Issue in original post is that you are doing use.foobar conditionally. Since use.register only executes on the first build, you’ll only register those two use.state on the first build, which is incorrect solution would be to hoist the use.states up before the use.register. You’ll still be able to access them within the register! |
Beta Was this translation helpful? Give feedback.
Issue in original post is that you are doing use.foobar conditionally. Since use.register only executes on the first build, you’ll only register those two use.state on the first build, which is incorrect
solution would be to hoist the use.states up before the use.register. You’ll still be able to access them within the register!