riverpod icon indicating copy to clipboard operation
riverpod copied to clipboard

[Suggestion] Convenience methods for MutationState (check for loading, success, error)

Open s9th opened this issue 7 months ago • 5 comments

Is your feature request related to a problem? Please describe. It's not a problem per se but eliminates the need of defining an extension. Also can be a first step making MutationState similar in usage to AsyncValue

Describe the solution you'd like Add the following methods to the MutationState class

bool get isLoading => this is PendingMutation;
bool get isSuccess => this is SuccessMutation;
bool get hasError => this is ErrorMutation;

s9th avatar May 03 '25 18:05 s9th

The main issue is, those don't upcast the type.

You can't do ``dart if (state.isSuccess) state.value.doSomething()

rrousselGit avatar May 03 '25 18:05 rrousselGit

in 90% of cases in my project I don't need the value itself. E.g. if I want to show a toast on successful mutation, or an error (without specifying the error). isLoading I also use on all kinds of buttons all the time and writing isLoading: mutation.state is PendingMutation just feels not riverpod-y :)

s9th avatar May 03 '25 19:05 s9th

I'll most definitely changes codegen mutations a little bit, to support non-codegen mutations anyway

rrousselGit avatar May 03 '25 19:05 rrousselGit

I also wanted to add that in the brief time that I worked with mutations, I already fell into a trap of writing mutation is PendingMutation instead of mutation.state a couple of times.

Btw mutations are a delight to work with. I've been busy rewriting and testing on one of my projects the whole day and it's a blast.

s9th avatar May 04 '25 06:05 s9th

Great suggestion. This further aligns MutationState with AsyncValue.

snapsl avatar May 04 '25 11:05 snapsl