Search code examples
reduxdartflutterredux-thunkflutter-redux

How to do periodic / scheduled actions using Flutter Redux?


I'm building a mobile app using Flutter, in which I use flutter_redux and redux_thunk. There's two kind of timed actions I want to achieve:

  1. Dispatch certain action every N seconds (repeated)

  2. Dispatch an action once after N seconds, likely from a thunk action (single run)

Are there any packages that wrap this logic? What would be your suggested way to achieve the two kind of scheduled actions?


Solution

  • I don't think there's anything special that you'd need to do specifically for Flutter Redux. In Dart, the general way to do periodic operations would be to use Timer.periodic:

    Timer.periodic(Duration(seconds: N), () => store.dispatch(action));
    

    For non-periodic operations, you could use a non-periodic Timer or use Future.delayed. (Timer gives you the ability to easily cancel it, but Future gives you a more direct way for the caller to specify how exceptions are handled.)