Search code examples
flutterblocconceptequatable

Information on Flutter Bloc pattern to reuse the state and event classes


I am using flutter_bloc package for state management and extends with Equatable. I have made Event class and state class for the data to display which I receive from api. It is the simple app where I am receiving data from api and just to display on the screen on UI. Everything works good. But now I also want to check the internet connectivity and display the snackBar if internet is not working. For this I will use Connectivity_plus package of flutter. But as I am using the Bloc pattern I already have one state class and one event class.

My question is, if now I would like to have the feature if Internet is working or not and if I want to implement in Bloc pattern then I have to create new NetworkState class and NetworkEvent class? OR I can reuse the state and event class which I already have in my app ??

In the general terms every time we have to make new states class and event class for the new feature or we can reuse these class for every feature which we implement in Bloc pattern in flutter?


Solution

  • The best practice is you should make separate state and event classes for each feature in your app. This can help keep the code organized and makes it easier to maintain and expand your application in the future. for example, in your situation, you can create a new NetworkEvent class that will fire when the application detects a change in network connectivity and a new NetworkState class that will contain the current network connection state. This way, you can keep your existing state and event classes focused on your application's existing functionality, and add new capabilities to networking functionality.