Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new widget block: BlocMuteSelector. Select a part of the state and create a mutable object. #4207

Conversation

MattiaPispisa
Copy link
Sponsor

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 the muteOrCopy function to execute the builder only when the state has actually changed, so muteWhen is used, which effectively works like BlocListener's listenWhen. The muteOrCopy 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. The create function is executed on the first render, while the muteOrCopy 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

  • [ X] ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • [ X] 📝 Documentation
  • 🗑️ Chore

…t allows you to select a part of the state and create a mutable object.
@felangel
Copy link
Owner

@MattiaPispisa can you please file an issue clearly describing the problem you’re facing so we can discuss and align on a solution? Thanks!

@MattiaPispisa
Copy link
Sponsor Author

Sure, I created the issue under "Feature Request" to discuss my problem, as it seemed like the most appropriate category among the available ones.

Issue

@felangel
Copy link
Owner

Closing this for now while we discuss the solution in the issue. I'll follow up this weekend, thanks!

@felangel felangel closed this Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants