Runner
Last updated
Last updated
A simple use case that executes a specific task which returns a Left Value
when an error occurs or a Right Value
when the task succeeds.
This is ideal for create, read, update and delete (CRUD) operations.
A Runner use case can emit 4 possible class states, all of which inherit from the RunnerState
sealed class:
A Runner also has the following properties that you can access:
Let's create a Runner use case named CountFruits
for categorizing and counting the number of fruits given in our fruit basket.
Create the following classes:
1. Create the Parameter class
This is the parameter class passed to the Runner
when being executed.
2. Create the Left value class
This is the object returned when the Runner
fails and emits a RunFailed
state:
This is the object returned when the Runner
succeeds and emits a RunSuccess
state:
Pass the Parameter
, Left
and Right
classes to the Runner
's generic arguments. Afterwards, implement the code logic in the onCall
method:
To start the runner, call the run
method:
The Runner will emit the Running
state, followed either by the RunSuccess
with a Right Value
or RunFailed
state with a Left Value
depending on the result of the runner.
The Runner gives you access to the following properties:
To clear the Runner and reset it back to its initial state, call the reset
method:
Every use case is a descendent of BLoC cubit. Hence, we can manage its states via the flutter_bloc package.
To run the Dart demo:
Clone to codenic_clean_arch repository.
Navigate to the packages/codenic_bloc_use_case
directory.
Run the example code:
Runner States | Description |
---|---|
Property | Description |
---|---|
RunnerInitial
The initial state or the state emitted when the use case has been reset.
Running
The state emitted when the task execution is in progress.
RunSuccess
The state emitted when the use case fails.
RunFailed
The state emitted when the use case succeeds.
value
The latest value returned when calling run()
. This may either be the leftValue
if RunFailed
was recently emitted. Otherwise, this will be equal to the rightValue
if RunSuccess
was more recent.
params
The latest params passed when calling run()
. This may either be the leftParams
if RunFailed
was recently emitted. Otherwise, this will be equal to the rightParams
if RunSuccess
was more recent.
leftValue
The last left value returned when a failed run()
was called.
leftParams
The last left params passed when a failed run()
was called.
rightValue
The last right value returned when a successful run()
was called.
rightParams
The last right params passed when a successful run()
was called.