Runner

Overview
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:
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.
A Runner also has the following properties that you can access:
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.
Creating a Runner
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:
3. Create the Right value class
This is the object returned when the Runner succeeds and emits a RunSuccess state:
4. Create the Runner
Pass the Parameter, Left and Right classes to the Runner's generic arguments. Afterwards, implement the code logic in the onCall method:
Using a Runner
1. Start the Runner
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.
2. View the Runner properties
The Runner gives you access to the following properties:
3. Reset the Runner
To clear the Runner and reset it back to its initial state, call the reset method:
Flutter Example
Every use case is a descendent of BLoC cubit. Hence, we can manage its states via the flutter_bloc package.
Dart Example
To run the Dart demo:
Clone to codenic_clean_arch repository.
Navigate to the
packages/codenic_bloc_use_casedirectory.Run the example code:
Last updated