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

Simpler Build When Method #1521

Closed
ianjaspersantos opened this issue Jul 24, 2020 · 3 comments · Fixed by #1886
Closed

Simpler Build When Method #1521

ianjaspersantos opened this issue Jul 24, 2020 · 3 comments · Fixed by #1886
Assignees
Labels
enhancement candidate Candidate for enhancement but additional research is needed pkg:flutter_bloc This issue is related to the flutter_bloc package
Projects

Comments

@ianjaspersantos
Copy link

I think bloc builder is good when it comes to rebuilding widget. I just want to say that I feel it is a bit boilerplatey especially in the Generics part, buildWhen method and the builder method part. First, I just think that can we eliminate the bloc/cubit generic since I only wanted the state? Second, I think this "previousState.count != currentState.count" condition is the most common condition we can have in buildWhen that can be easily be replaceable with the propose feature below. For more complex condition I think its suitable to use BlocBuilder Widget.

class CounterText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CounterCubit, CounterState>(
      buildWhen: (previousState, currentState) => previousState.count != currentState.count,
      builder: (context, state) {
        final count = state.count;

        return Text(
          '$count',
          style: Theme.of(context).textTheme.headline4,
        );
      },
    );
  }
}

I think this easily manageable code when we have to many widgets with the same pattern unlike above? This should only rebuild when only the state.count changes and will not be affected by other state changes.

class CounterText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final count = context.blocSelect((CounterState state) => state.count);

    return Text(
      '$count',
      style: Theme.of(context).textTheme.headline4,
    );
  }
}

Underlying Signature

extension BlocSelectContext on BuildContext {
      R blocSelect<T, R>(R selector(T value)) {}
}
@felangel
Copy link
Owner

Hi @ianjaspersantos 👋
Thanks for opening an issue!

Unfortunately we cannot remove the bloc/cubit generic because without it the BlocBuilder wont' be able to do the lookup for you (BlocProvider.of<T>(context)).

I have been thinking about having a selector and will spend some time on it once I wrap up the documentation updates for v6.0.0 😄

@felangel felangel self-assigned this Jul 24, 2020
@felangel felangel added enhancement candidate Candidate for enhancement but additional research is needed pkg:flutter_bloc This issue is related to the flutter_bloc package labels Jul 24, 2020
@felangel felangel added this to To do in bloc via automation Jul 24, 2020
@ianjaspersantos
Copy link
Author

Thanks Fel! Thank you for all the effort making flutter coding as great as possible 😁

@marcin-jelenski
Copy link

Selectors would be a way to solve #1512 even nicely 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement candidate Candidate for enhancement but additional research is needed pkg:flutter_bloc This issue is related to the flutter_bloc package
Projects
bloc
  
Done
Development

Successfully merging a pull request may close this issue.

3 participants