<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by filippo zazzeroni on Medium]]></title>
        <description><![CDATA[Stories by filippo zazzeroni on Medium]]></description>
        <link>https://medium.com/@filippo.zazzeroni?source=rss-b0f98fd6651a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*FRmwSjBfGEqb3xUh</url>
            <title>Stories by filippo zazzeroni on Medium</title>
            <link>https://medium.com/@filippo.zazzeroni?source=rss-b0f98fd6651a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:01:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@filippo.zazzeroni/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Better BLOC flutter state management]]></title>
            <link>https://medium.com/good-code/better-bloc-flutter-state-management-f7346e7a3386?source=rss-b0f98fd6651a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7346e7a3386</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[software-design]]></category>
            <dc:creator><![CDATA[filippo zazzeroni]]></dc:creator>
            <pubDate>Wed, 02 Feb 2022 15:34:38 GMT</pubDate>
            <atom:updated>2023-11-28T13:42:51.470Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everyone, I am Filippo Zazzeroni. I have been a software engineer since three years, focusing mainly on mobile development, in Flutter and Native, both iOS and Android. I write this article because I think Flutter has great potential and need to be understood in order to fully exploit its abilities. What I think about this technology is that it is very powerful and can lead to really great results in a short time.</p><p>My motto, and I think which is shared among community of developers, is that if the wheel already exists, it shouldn’t be invented again. But with Flutter, everything changes. This motto is no longer applicable and is replaced by strong software designing skills and dedication.</p><p>My approach with Flutter has continuously changed over time, because new knowledge was being added as days went on. As the team leader of MasterBiga, a startup, with the aim to create technology for non-industrial bakery and pizzeria, I decided to apply this development cycle to our flutter products. We divided our main application into microservices that could live independently of each other. This was developed and maintained by the internal team. All the plugins were developed with the scope of reducing external dependencies to already existing flutter packages, like BLOC.</p><p><a href="https://www.masterbiga.cloud/">MasterBiga</a></p><p>BLOC library was redesigned from scratch to achieve better results and to not adapt to already existing flutter bloc plugin.</p><h3><strong>BLOC plugin redesign</strong></h3><p>The redesign led to some important results. The first result was with the BlocProvider class that originally uses a context in order to provide the specified bloc to the class. Now the bloc provider doesn’t use context anymore to retrieve blocs, instead it uses a Singleton manager class that simply stores blocs in a list and it filters them to retrieve the selected one.</p><p>Here, we can see that project structure of bloc redesign:</p><pre>-- bloc:<br> - bloc_provider.dart<br> - bloc_builder.dart<br> - bloc.dart<br> - bloc_coordinator.dart<br> - bloc_state.dart<br> - state_manageable.dart<br> - provider_manager.dart<br> - bloc_initialiser.dart</pre><p><strong>Bloc</strong></p><pre>import &#39;dart:async&#39;;<br>import &#39;package:payments/models/interfaces/bloc/provider_manager.dart&#39;;<br>import &#39;bloc_coordinator.dart&#39;;<br>import &#39;bloc_state.dart&#39;;<br><br>abstract class Bloc<em>&lt;</em>T extends BlocState, C extends BlocCoordinator<em>&gt; {<br><br>  </em>abstract final BlocCoordinator coordinator;<br><br>  Stream<em>&lt;</em>C<em>&gt; </em>get stream;<br><br>  T get state;<br><br>  void setState<em>(</em>T state<em>) {<br>    </em>coordinator.setState<em>(</em>state<em>)</em>;<br>  <em>}<br><br>  </em>static T <em>ofType&lt;</em>T extends Bloc<em>&gt;() {<br>    </em>return ProviderManager.<em>instance</em>.read<em>&lt;</em>T<em>&gt;()</em>;<br>  <em>}<br><br>}</em></pre><p>Here is the Bloc class: it defines the foundation of a bloc. Bloc holds a coordinator who coordinates the states of the bloc. The bloc provide a class method to read the required bloc from the provider manager. The bloc is able to set its state and get a stream of its coordinator object.</p><p><strong>Bloc Coordinator</strong></p><pre>import &#39;dart:async&#39;;<br>import &#39;bloc_state.dart&#39;;<br><br>abstract class BlocCoordinator<em>&lt;</em>State extends BlocState<em>&gt; {<br><br>  </em>BlocCoordinator<em>() {<br>    </em>onControllerFirstListenCallback<em>()</em>;<br>  <em>}<br><br>  </em>State getState<em>()</em>;<br><br>  void setState<em>(</em>State state<em>)</em>;<br><br>  abstract final State state;<br><br>  abstract final StreamController<em>&lt;</em>BlocCoordinator<em>&gt; </em>controller;<br><br>  void onControllerFirstListenCallback<em>()</em>;<br><em>}</em></pre><p>The coordinator object holds the bloc state and a stream of itself. It is able to set the state of the bloc.</p><p><strong>Provider Manager</strong></p><pre>import &#39;package:payments/models/interfaces/bloc/bloc.dart&#39;;<br><br>class ProviderManager <em>{<br><br>  </em>ProviderManager._internal<em>()</em>;<br><br>  static final <em>instance </em>= ProviderManager._internal<em>()</em>;<br><br>  final _providers = <em>[]</em>;<br><br>  void save<em>&lt;</em>T extends Bloc<em>&gt;(</em>T provider<em>) {<br>    </em>_providers.add<em>(</em>provider<em>)</em>;<br>  <em>}<br><br>  </em>T read<em>&lt;</em>T extends Bloc<em>&gt;() {<br>    </em>return _providers.whereType<em>&lt;</em>T<em>&gt;()</em>.first;<br>  <em>}<br><br>}</em></pre><p>This manager implements Singleton pattern, and is able to save and read a Bloc from the providers lists.</p><p><strong>Bloc Provider</strong></p><pre>import &#39;package:payments/models/interfaces/bloc/provider_manager.dart&#39;;<br><br>import &#39;bloc.dart&#39;;<br><br>class BlocProvider<em>&lt;</em>T extends Bloc<em>&gt; {<br>  </em>void create<em>(</em>T bloc<em>) {<br>    </em>ProviderManager.<em>instance</em>.save<em>(</em>bloc<em>)</em>;<br>  <em>}<br>}</em></pre><p>The bloc provider is very simple, and delete any trace of the context. It can only create a bloc and save it in the provider list.</p><p><strong>State Manageable</strong></p><pre>mixin StateManageable <em>{<br>  </em>Widget blockBuilder<em>&lt;</em>T extends Bloc, V extends BlocCoordinator<em>&gt;(</em>Widget Function<em>(</em>BuildContext,AsyncSnapshot<em>&lt;</em>BlocCoordinator<em>&gt;) </em>builder<em>) {<br>    </em>return BlocBuilder<em>&lt;</em>T ,V<em>&gt;(</em>builder: builder<em>)</em>;<br>  <em>}<br>}</em></pre><p>This mixin helps use this plugin in a flexible way like the example below:</p><pre>class TestView extends StatelessWidget with StateManageable <em>{<br>  </em>const TestView<em>({</em>Key? key<em>}) </em>: super<em>(</em>key: key<em>)</em>;<br><br>  @override<br>  Widget build<em>(</em>BuildContext context<em>) {<br>    </em>return BlocBuilder<em>&lt;</em>DatabaseUpdateNotifier,DatabaseUpdateStateCoordinator<em>&gt;(<br>        </em>builder: <em>(</em>context, data<em>) {<br>          </em>return const Text<em>(</em>&quot;test&quot;<em>)</em>;<br>        <em>}</em>,<em>)</em>;<br>  <em>}<br>}</em></pre><p><strong>Bloc Initialiser</strong></p><pre>mixin BlocInizitaliserable <em>{<br>  </em>void initialiser<em>({</em>required Bloc bloc<em>}) {<br>    </em>BlocProvider<em>()</em>.create<em>(</em>bloc<em>)</em>;<br>  <em>}<br>}</em></pre><p>Mixins are very flexible way to integrate features in class and for this reason I use mixin to expose the functionality of the package.</p><p>Here is an example:</p><pre>class TestView extends StatefulWidget  <em>{<br>  </em>const TestView<em>({</em>Key? key<em>}) </em>: super<em>(</em>key: key<em>)</em>;<br><br>  @override<br>  State<em>&lt;</em>TestView<em>&gt; </em>createState<em>() </em>=&gt; _TestViewState<em>()</em>;<br><em>}<br><br></em>class _TestViewState extends State<em>&lt;</em>TestView<em>&gt; </em>with StateManageable, BlocInitialiser <em>{</em></pre><pre>  @override<br>  void initState() {</pre><pre>    initialiser(bloc: DatabaseUpdateNotifier())</pre><pre>  }</pre><pre><em>  </em>@override<br>  Widget build<em>(</em>BuildContext context<em>) {<br>    </em>return BlocBuilder<em>&lt;</em>DatabaseUpdateNotifier, DatabaseUpdateStateCoordinator<em>&gt;(<br>        </em>builder: <em>(</em>context, data<em>) {<br>          </em>return const Text<em>(</em>&quot;test&quot;<em>)</em>;<br>        <em>}</em>,<em>)</em>;<br>  <em>}<br>}</em></pre><p><strong>Application to Database Update Notifier</strong></p><p>This bloc listens to update on the database and when new data are added, it notifies the bloc builders that listen to this bloc.</p><pre>class DatabaseUpdateNotifier extends Bloc<em>&lt;</em>DatabaseUpdateState, DatabaseUpdateStateCoordinator<em>&gt; {<br><br><br>  </em>final _coordinator = DatabaseUpdateStateCoordinator<em>()</em>;<br><br>  void updateState<em>(</em>DatabaseUpdateState newState<em>) {<br>    </em>if <em>(</em>newState != coordinator.getState<em>()) {<br>      </em>coordinator.controller.sink.add<em>(</em>coordinator<em>)</em>;<br>    <em>}<br>  }<br><br>  </em>@override<br>  BlocCoordinator<em>&lt;</em>BlocState<em>&gt; </em>get coordinator =&gt; _coordinator;<br><br>  @override<br>  DatabaseUpdateState get state =&gt; _coordinator.getState<em>()</em>;<br><br>  @override<br>  Stream<em>&lt;</em>DatabaseUpdateStateCoordinator<em>&gt; </em>get stream =&gt; _coordinator.controller.stream as Stream<em>&lt;</em>DatabaseUpdateStateCoordinator<em>&gt;</em>;<br><br><em>}<br><br></em>class DatabaseUpdateStateCoordinator<em>&lt;</em>T extends DatabaseUpdateState<em>&gt; </em>implements BlocCoordinator<em>&lt;</em>DatabaseUpdateState<em>&gt; {<br><br>  </em>@override<br>  DatabaseUpdateState getState<em>() {<br>    </em>state = DatabaseUpdateState<em>(</em>DatabaseUpdateState.<em>noUpdates)</em>;<br>    controller.add<em>(</em>this<em>)</em>;<br>    return state;<br>  <em>}<br><br>  </em>@override<br>  StreamController<em>&lt;</em>DatabaseUpdateStateCoordinator<em>&gt; </em>controller = StreamController<em>&lt;</em>DatabaseUpdateStateCoordinator<em>&gt;</em>.broadcast<em>()</em>;<br><br>  @override<br>  void onControllerFirstListenCallback<em>() {<br>    </em>controller.onListen = <em>() {<br>      </em>controller.add<em>(</em>this<em>)</em>;<br>    <em>}</em>;<br>  <em>}<br><br>  </em>@override<br>  DatabaseUpdateState state = DatabaseUpdateState<em>(</em>DatabaseUpdateState.<em>noUpdates)</em>;<br><br>  @override<br>  void setState<em>(</em>DatabaseUpdateState state<em>) {<br>    </em>this.state = state;<br>    controller.add<em>(</em>this<em>)</em>;<br>  <em>}<br><br>}<br><br></em>class DatabaseUpdateState extends BlocState <em>{<br><br>  </em>DatabaseUpdateState<em>(</em>this.rawValue<em>)</em>;<br><br>  static const <em>noUpdates </em>= &quot;noUpdates&quot;;<br><br>  static const <em>updatesInQueue </em>= &quot;updatesInQueue&quot;;<br><br>  @override<br>  final String rawValue;<br><br><em>}</em></pre><p>In this article, I have tried to show an example of the Database Update Notifier with the support of the Bloc package.</p><p>Thank you for reading. This, for me, is first of many articles to come. I can’t wait to share my knowledge and publish new resources.</p><ul><li><a href="https://www.linkedin.com/in/filippo-zazzeroni-0306a81bb/">Filippo Zazzeroni - Founder, Solidity developer - BRO Finance | LinkedIn</a></li><li><a href="https://www.masterbiga.cloud/">MasterBiga</a></li><li><a href="https://pub.dev/packages/flutter_bloc">flutter_bloc | Flutter package</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7346e7a3386" width="1" height="1" alt=""><hr><p><a href="https://medium.com/good-code/better-bloc-flutter-state-management-f7346e7a3386">Better BLOC flutter state management</a> was originally published in <a href="https://medium.com/good-code">Good Code</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>