A new widget block: BlocMuteSelector. Select a part of the state and create a mutable object. #4207
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A new widget block: BlocMuteSelector. Based on the state of a bloc, it allows you to select a part of the state and create a mutable object.
Status
READY
Breaking Changes
NO
Description
A use case I encounter in some situations (especially when creating complex forms) is having an object that changes over time but should not reside in the bloc state. For example, I have TextFormFields that require using a TextEditingController because their content can change due to actions outside the field itself. Recreating the TextEditingController would necessitate repositioning the cursor correctly and dealing with other issues. By creating a mutable state, I can create and modify a TextEditingController. The
muteOrCopy
function takes the previous mutable state and the current bloc state as inputs, allowing me to apply controls based on the previous value. Since it is mutable, you cannot rely on themuteOrCopy
function to execute thebuilder
only when the state has actually changed, somuteWhen
is used, which effectively works like BlocListener's listenWhen. ThemuteOrCopy
function has a dual purpose; if it returns null, the state is expected to be mutable, but if it returns a new state, a comparison between the previous and new state is applied to update only if it has actually changed. Thecreate
function is executed on the first render, while themuteOrCopy
function is applied for all subsequent renders.Another use case where this can be helpful is when ValueNotifiers are required to satisfy third-party libraries, and I do not want to change the bloc state based on new third-party constraints.
Type of Change