deprecate: BlocOverrides API #3470
Labels
deprecation
A public API is being deprecated
pkg:bloc
This issue is related to the bloc package
pkg:hydrated_bloc
This issue is related to the hydrated_bloc package
Overview
The
BlocOverrides
API was introduced in v8.0.0 in an attempt to support scoping bloc-specific configurations such asBlocObserver
,EventTransformer
, andHydratedStorage
. In pure Dart applications, the changes worked well; however, in Flutter applications the new API caused more problems than it solved.The
BlocOverrides
API was inspired by similar APIs in Flutter/Dart:Problems
While it wasn't the primary reason for these changes, the
BlocOverrides
API introduced additional complexity for developers. In addition to increasing the amount of nesting and lines of code needed to achieve the same effect, theBlocOverrides
API required developers to have a solid understanding of Zones in Dart.Zones
are not a beginner-friendly concept and failure to understand how Zones work could lead to the introduction of bugs (such as uninitialized observers, transformers, storage instances).For example, many developers would have something like:
The above code, while appearing harmless, can actually lead to many difficult to track bugs. Whatever zone
WidgetsFlutterBinding.ensureInitialized
is initially called from will be the zone in which gesture events are handled (e.g.onTap
,onPressed
callbacks) due toGestureBinding.initInstances
. This is just one of many issues caused by usingzoneValues
.In addition, Flutter does many things behind the scenes which involve forking/manipulating Zones (especially when running tests) which can lead to unexpected behaviors (and in many cases behaviors that are outside the developer's control -- see issues below).
Due to the use of the runZoned, the transition to the
BlocOverrides
API led to the discovery of several bugs/limitations in Flutter (specifically around Widget and Integration Tests):which affected many developers using the bloc library:
BlocOverrides
add support forrunZonedGuarded
#3394runZoned
#3319Decision
After many discussions with various members of the community (including folks from the Dart/Flutter teams), we've decided to deprecate the
BlocOverrides
API and reintroduce the previous APIs in order to provide a safe, beginner friendly API, eliminate the need to worry about the above issues. When usingZones
, technically there's nothing stopping another package from overriding the Zone values provided or even creating a brand new Zone which does not inherit the previous zone specifications. By moving back to the previous API, we eliminate these potential pit-falls and end up with a safer, simpler API.Next Steps
The
BlocOverrides
andHydratedBlocOverrides
APIs will be deprecated, the previousBloc.observer
,Bloc.transformer
, andHydratedBloc.storage
APIs will be restored, and the deprecated APIs will be removed in the next major release.Migration
Currently, in order to specify a custom
BlocObserver
orEventTransformer
we use theBlocOverrides.runZoned
API:Before
After these changes, you will be able to use the previous APIs (from v7.x.x) to specify a custom
BlocObserver
,EventTransformer
, orHydratedStorage
instance.After
The text was updated successfully, but these errors were encountered: