TryReturn class
An alias for Try/Call
effect. It makes try/catch/finally code blocks more readable and returns value like Return.
If you want to return from a returned value from a Try/Catch/Finally block then use TryReturn
.
Example
In the following example the saga returns value returned
saga() sync* {
yield TryReturn(() sync* { //returns saga
//...
yield Return(somevalue1); //returns Try
}, Catch: (e, s) sync* {
//...
yield Return(somevalue2); //returns Try
});
}
Equivalent code with Try
saga() sync* {
var result = Result();
yield Try(() sync* {
//...
yield Return(somevalue1); //returns Try. Does not return saga
}, Catch: (e, s) sync* {
//...
yield Return(somevalue2); //returns Try. Does not return saga
}, result: result);
yield Return(result.value); //returns saga
}
In the following example, there is a simple checkout saga function.
It tries to checkout the cart. If it successes returns a CheckoutSuccess
action
otherwise returns a CheckoutFailure
action.
import 'package:redux_saga/redux_saga.dart';
import 'Api.dart';
//...
checkout() sync* {
yield TryReturn(() sync* {
var cart = Result<Cart>();
yield Select(selector: getCart, result: cart);
yield Call(buyProductsAPI, args: [cart.value]);
yield Return(CheckoutSuccess(cart.value));
}, Catch: (e, s) sync* {
yield Return(CheckoutFailure(error));
});
}
- Inheritance
-
- Object
- Effect
- EffectWithResult
- Call
- TryReturn
Constructors
Properties
- args → List?
-
Arguments of the function to call
finalinherited
- Catch → Function?
-
A Generator function or a normal function to invoke for uncaught errors.
finalinherited
- Finally → Function?
-
A Generator function or a normal function to invoke in any case after call.
finalinherited
- fn → Function
-
A Generator function or a normal function to call.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- name → String?
-
Meta name of function
finalinherited
-
namedArgs
→ Map<
Symbol, dynamic> ? -
Named arguments of the function to call
finalinherited
- result ↔ Result?
-
Result after effect is resolved.
getter/setter pairinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getDefinition(
) → Map< String, dynamic> -
Returns all properties of effect as a dictionary object
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited