felangel / felangel/bloc

feat: Add optional type argument to represent "from state" in `on` method

Open
#4,217 5 comments 2 reactions 0 assignees View on GitHub
enhancement candidate pkg:bloc
Dominant language
Dart
Stars
12.5k
Forks
3.4k
PR merge metrics
No merged PRs in 30d

Description

**Description**

In a state machine, state transitions occur from a particular state to another state. Currently, the `on<...>` method in BLoC does not provide an explicit way to specify the "from state" for a transition. The proposal suggests adding an optional type argument to the `on<...>` method to specify the state from which the transition is happening.

**Motivation**

Currently, BLoC code allows developers to identify the event that caused a transition (from the `on` method) and the state being transitioned to (from the `emit(state)` calls). However, the "from state" information is not explicitly captured, making it challenging to model the state machine directly from BLoC code. Developers often have to implement their own guardrails (e.g., with assertions or type casts) to enforce specific state transitions or rely on implicit assumptions about the BLoC's usage. Adding the "from state" parameter would eliminate the need for these custom guardrails, providing a complete picture of the state transitions directly within the BLoC code. This enhancement would also enable easy generation of visualizations of the state machine by parsing the on<> and emit calls..

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.