What’s New in flutter_bloc 0.19.0

Felix Angelov
Jul 11, 2019 · 4 min read
Image for post
Image for post

Hey everyone! I’m excited to announce flutter_bloc v0.19.0 which includes several features requested by the community with an emphasis on developer experience improvements and boilerplate reduction.

Before jumping into the updates, I’d like to give a big thank you to everyone who gave feedback, created PRs, and reported issues; we couldn’t have done it without your contributions.

Now without further ado, let’s check out what’s new!

BlocProvider Improvements

There are currently two ways to use BlocProvider.

You can instantiate and provide a bloc using the default constructor:

Previously we’d have to do something like this:

As you can see, we no longer have to worry about disposing our blocs! As long as they are created by BlocProvider they will be disposed by BlocProvider automatically 🎉.

The second way to use BlocProvider is using the value constructor:

In the above snippet, we are making an existing instance of MyBloc available to the MyPage widget which is being pushed as a new route via the Navigator. In this case, we’re providing an existing instance of MyBloc and we don’t want MyBloc to be disposed when MyPage is disposed because it is being used in other parts of the application. As a result, we can use the value constructor in order to provide an existing bloc instance to a widget and the bloc will not be disposed. You can check out the route access recipe for more details.

To recap, if you’re creating and providing a new bloc instance to a subtree, you can use the default constructor with builder to create the bloc instance and BlocProvider will handle automatically dispose the bloc. If you want to provide an existing bloc instance to a subtree, you can use the value constructor of BlocProvider and BlocProvider will not dispose the bloc automatically (leaving it up to the BlocProvider higher up in the widget tree which created the bloc to dispose it).

You should no longer need to write any StatefulWidgets for managing your blocs 🎉

MultiBlocProvider (was BlocProviderTree)

MultiBlocProvider improves the readability and eliminates the need to nest multiple BlocProviders.

By using MultiBlocProvider we can go from:

to:

In both cases, we are providing BlocA, BlocB, and BlocC to ChildA; however, with MultiBlocProvider we can do so without the additional nesting which helps keep the code easy to read.

Repository Provider (was ImmutableProvider)

At a high level a repository is a wrapper around one or more data providers with which a bloc communicates. As a result, it’s important to be able to provide and access repositories throughout the widget tree and with the latest updates it’s easier than ever.

To create and provide a new repository we can use the default RepositoryProvider constructor like so:

In the above snippet, we are making an instance of MyRepository available to MyChild and its subtree. As a result, further down the widget tree we can create an instance of MyBloc (which has a dependency on MyRepository) by accessing the instance of MyRepository using RepositoryProvider.of. You’ll notice the API is very similar to that of BlocProvider with an attempt to make the library as intuitive and easy to use as possible.

MultiRepositoryProvider (was ImmutableProviderTree)

MultiRepositoryProvider improves the readability and eliminates the need to nest multiple RepositoryProviders.

By using RepositoryProvider we can go from:

to:

In both cases, we are providing RepositoryA, RepositoryB, and RepositoryC to ChildA; however, with MultiRepositoryProvider we can do so without the additional nesting which helps keep the code easy to read.

Integration with Provider

flutter_bloc will still continue to have specialized providers (as it’s an opinionated library) but we’re excited to be using provider so that we don’t have to maintain our own InheritedWidget wrapper and so we can benefit from all future improvements/features that come with upcoming provider releases.

You can check out the complete change log for more information and as always you can support me by ⭐️the repository, or 👏 for this story.

Flutter Community

Articles and Stories from the Flutter Community

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store