How to access Provided (Provider.of()) value inside showModalBottomSheet?

InheritedWidgets, and therefore Providers, are scoped to the widget tree. They cannot be accessed outside of that tree. The thing is, using showDialog and similar functions, the dialog is located in a different widget tree – which may not have access to the desired provider. It is therefore necessary to add the desired providers in … Read more

MVVM vs Bloc patterns

Looking at this illustration for MVVM (source): You can see that there are seperate data and business logic models. However, using BLoC there is not really a distinction like that. The classes that handle the business logic also handle the data, which can also apply to MVVM. To be fair, there really is not much … Read more

BlocProvider.of() called with a context that does not contain a Bloc – even that it does

You can just wrap the Blocs you need to access through out the app by wrapping it at the entry point of the app like this runApp( MultiBlocProvider( providers: [ BlocProvider<UserBloc>( create: (context) => UserBloc(UserRepository()), ), ], child: App() ) ); } and you can access this bloc at anywhere of your app by BlocProvider.of<UserBloc>(context).add(event … Read more

flutter: Unhandled Exception: Bad state: Cannot add new events after calling close

Use StreamController.isClosed to check if the controller is closed or not, if not closed add data to it. if (!_controller.isClosed) _controller.sink.add(…); // safe to add data as _controller isn’t closed yet From Docs: Whether the stream controller is closed for adding more events. The controller becomes closed by calling the close method. New events cannot … Read more

Bloc, Flutter and Navigation

To get the myth of BLoC being the way forward right out of the way: There is no perfect way for handling state. Every state management architecture solves some problems better than others; there are always trade-offs and it’s important to be aware of them when deciding on an architecture. Generally, good architecture is practical: … Read more

tech