You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So, when needed, I can return const Left(NumberOutOfRangeFailure(minValue: 1, maxValue: 99)), meaning: you can only chose between numbers 1 and 99
Now, how can I display an error message to the user, since my method returns the base Failure class, without using if or switch?
What about:
staticvoid_dispatchCommand({requiredBuildContext context, requiredCommand command}) {
final result = command.dispatch(context);
result.leftMap(
(failure) {
final errorMessage =match<Failure, String>(failure)
.when<GetNumberedQuoteCommandEmptyNumberFailure>((f) =>"Number should not be empty")
.when<GetNumberedQuoteCommandInvalidNumberFormatFailure>((f) =>"Number is not correctly formatted")
.when<NumberShouldBePositiveFailure>((f) =>"Number should be positive")
.when<NumberShouldBeLessThan100Failure>((f) =>"Number should be < 100")
.when<NumberOutOfRangeFailure>((f) =>"Number should be between ${f.minValue} and ${f.maxValue}")
.otherwise((f) =>"Unknown ${f} error");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:Text(errorMessage),
),
);
},
);
}
In the above code, I just ignore the Success (right), because, well, I don't need to inform the user about it, but for Failure (left), I can test it using generics and have typed failures to create my error message string (notice how I interpolate the minValue and maxValue in the error message).
It sounds like your problem could easily be solved by making Failure a sealed class and using a switch statement. But, if you don't want to do that, you could add an abstract method to Failure, which would require all implementing classes to override said method.
abstractclassFailure {
StringgetFailureMessage();
...
}
classNumberOutOfRangeFailureextendsFailure {
finalint minValue = ...; // Get these values however you wantfinalint maxValue = ...;
@overrideStringgetFailureMessage() {
return"Number should be between ${minValue} and ${maxValue}";
}
}
Then you can just use any instance of a subclass of Failure to call getFailureMessage().
Scenario:
Either<Failure, Success>
.Failure
is a base class for "I don't have a clue what happened"Failure
to be more precise, for instance:const Left(NumberOutOfRangeFailure(minValue: 1, maxValue: 99))
, meaning: you can only chose between numbers 1 and 99Failure
class, without usingif
orswitch
?What about:
In the above code, I just ignore the
Success
(right), because, well, I don't need to inform the user about it, but forFailure
(left), I can test it using generics and have typed failures to create my error message string (notice how I interpolate the minValue and maxValue in the error message).How?
Using this code (based on #48):
So now I can filter my result (by
bool
predicate), or I can match types using mywhen
extension.Anyway, my feature request is to incorporate that
when
,on
andotherwise
intoEither<L, R>
.The text was updated successfully, but these errors were encountered: