Skip to content

best practice for updating state in AsyncNotifier class #2423

Answered by AhmedLSayed9
mafreud asked this question in Q&A
Discussion options

You must be logged in to vote
  • state.when: can be used to update state only if current state is something (i.e you can ignore updating if current state is loading/error). Btw, In your sample loading/error flags do nothing beside just returning current error/loading.

  • update: close to state.when but if current state is loading, the update will be awaited and called if data is emitted later. If current state is error, the callback will not be invoked (onError can be used to handle if current state is error).

Note: Both state.when & update don't try/catch errors and typically used when your update is sync. If your update is async and does throw errors, that's when AsyncGuard is preferred.

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
4 replies
@mafreud
Comment options

@AhmedLSayed9
Comment options

@mafreud
Comment options

@yknevenky
Comment options

Answer selected by mafreud
Comment options

You must be logged in to vote
2 replies
@fenrirx22
Comment options

@SiddiquiMonis401
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants