BLoC Is Moving Biz Logic From
BLoC does not get rid of the combined controller and view in extends State<MyWidget>. Instead it sets up a Model-View that is bound to the State class to handle most of the controller logic that we would put in the non BLoC State class.
/// {@template bloc_base} /// An interface for the core functionality implemented by /// both [Bloc] and [Cubit]. /// {@endtemplate} abstract class BlocBase<State> implements StateStreamableSource<State>, Emittable<State>, ErrorSink { /// {@macro bloc_base} BlocBase(this._state) { // ignore: invalid_use_of_protected_member _blocObserver.onCreate(this); } // ignore: deprecated_member_use_from_same_package final _blocObserver = BlocOverrides.current?.blocObserver ?? Bloc.observer; late final _stateController = StreamController<State>.broadcast(); State _state; bool _emitted = false; @override State get state => _state; @override Stream<State> get stream => _stateController.stream; /// Whether the bloc is closed. /// /// A bloc is considered closed once [close] is called. /// Subsequent state changes cannot occur within a closed bloc. @override bool get isClosed => _stateController.isClosed; /// Updates the [state] to the provided [state]. /// [emit] does nothing if the [state] being emitted /// is equal to the current [state]. /// /// To allow for the possibility of notifying listeners of the initial state, /// emitting a state which is equal to the initial state is allowed as long /// as it is the first thing emitted by the instance. /// /// * Throws a [StateError] if the bloc is closed. @protected @visibleForTesting @override void emit(State state) { try { if (isClosed) { throw StateError('Cannot emit new states after calling close'); } if (state == _state && _emitted) return; onChange(Change<State>(currentState: this.state, nextState: state)); _state = state; _stateController.add(_state); _emitted = true; } catch (error, stackTrace) { onError(error, stackTrace); rethrow; } } /// Called whenever a [change] occurs with the given [change]. /// A [change] occurs when a new `state` is emitted. /// [onChange] is called before the `state` of the `cubit` is updated. /// [onChange] is a great spot to add logging/analytics for a specific `cubit`. /// /// **Note: `super.onChange` should always be called first.** /// ```dart /// @override /// void onChange(Change change) { /// // Always call super.onChange with the current change /// super.onChange(change); /// /// // Custom onChange logic goes here /// } /// ``` /// /// See also: /// /// * [BlocObserver] for observing [Cubit] behavior globally. /// @protected @mustCallSuper void onChange(Change<State> change) { // ignore: invalid_use_of_protected_member _blocObserver.onChange(this, change); } /// Reports an [error] which triggers [onError] with an optional [StackTrace]. @protected @mustCallSuper @override void addError(Object error, [StackTrace? stackTrace]) { onError(error, stackTrace ?? StackTrace.current); } /// Called whenever an [error] occurs and notifies [BlocObserver.onError]. /// /// **Note: `super.onError` should always be called last.** /// /// ```dart /// @override /// void onError(Object error, StackTrace stackTrace) { /// // Custom onError logic goes here /// /// // Always call super.onError with the current error and stackTrace /// super.onError(error, stackTrace); /// } /// ``` @protected @mustCallSuper void onError(Object error, StackTrace stackTrace) { // ignore: invalid_use_of_protected_member _blocObserver.onError(this, error, stackTrace); } /// Closes the instance. /// This method should be called when the instance is no longer needed. /// Once [close] is called, the instance can no longer be used. @mustCallSuper @override Future<void> close() async { // ignore: invalid_use_of_protected_member _blocObserver.onClose(this); await _stateController.close(); } }
That is the BLoC base class with State bound to it. Subscribe to my substack to go beyond the non clear Flutter Docs.
Free
Flutter Localization Without Writing It Twice
Google's Flutter SDK way of locaization has you writing code
twice. There is a better way that the Flutter GDE's are not covering.
Material Design Holes In Flutter SDK And The Fix
Every edition of Material Design has implementation holes that the
Flutter SDK and GDE's do not cover.
A Design And DevOPS Way Of Generating Flutter Launch Icons
As of Android 12 the way to get native splash is to auto generate
the launch icons. This is a better way to do it and includes how
to generate the Material You launch monochrome icons.
Beating M3 IN A Flutter Dev Machine
This is the intel way to out perform Apple M-silicon.
How To Implement Observable Flutter Logging
This is how to get full application logging beyond the debug mode.
Better App Exceptions Catching Than The Flutter SDK
A better way to catch application exceptions that extends
the Flutter SDK way of catching exceptions.
Flutter Static Code Analysis Tools To Pair For DevOPS
The best way to judge code architecture decisions is through
these static analysis tools.
What ADHD Designers Are Hding From You About Super Focus
A better way to code and design focus.
How To Implement Flutter Staggered Animations
Staggered animation of container contents is in the Material
Design 3 spec but not the flutter SDK. This is one of the
ways to get staggered animations.
New In Flutter 3 Sliver Axis Groups
This is some new Sliver layout tools that made it into the Flutter SDK.
Why The Flutter SDK Sucks
The pain of dealing with the fast developing Flutter SDK.
What StoryBoarding In Flutter Will Google introduce At IO 2024
This the only storyboarding package in which you get to use the
same code to do BDD unit testing. So it is a must read.
Design The Data Class Model Instead Of Coding It
This complements using Stager as your storyboard library in that
mock methods should always be in your Models.
How To Write Flutter Adaptive Scaffold Breakpoints Like You Were A GDE
First step to writing an adaptive scaffold is coding Material Design 3
breakpoints since they are not in the Flutter SDK.
Fixing Flutter Surface Color Roles
Material Design 3 spec recently changed to having surface color roles.
This is a way to implement them before the Flutter SDK has them.
Flutter GDE Way To Code A Side Sheet
One of the changes from Material Design 2 to Material Design 3 is that
the navigation drawers are now wrapped in side sheets.
Secret Scroll tweak Not In The Flutter Docs
While we do not want scroll in our fields we need to locally
refix scroll for the non mobile platforms, this is how to do that.
领英推荐
Playing With PageView Scroll Physics
Differnt pageview scroll physics you can implement.
Parallax Via Flutter Scrollable
Its parallaxs effect is all tied to the scrollable.
Awesome Flutter Theme Animation
This is the way to do using the fact that one can create an image
of what is rendered.
How To Get Variable Fonts In Flutter
This is the way to use custom variable fonts in flutter.
Designers Do Not Let Flutter Designers use UseCase classes as Services
Stop abstracting stuff that does not matter such a UseCase classes, please!
Design Hacking Flutter HCT Color Space
A design twist in getting brand colors into HCT
Do Not Use debugPrint In Flutter
Yes, avoid using print and debugPrint..this is why
How To Upgrade Color Scheme When Material Design Spec Changes
Easy way to upgrade Color Scheme when the spec changes.
PAID
Missing In Flutter SDK Docs Your Axis Layout Guide
This one of the layout guides you need. And the other one is in
process and will be out soon.
IOS Squircle Shapes For Flutter
No, the Flutter SDK shape stadium does not even come close to
matching iOS squircle. This is my implementation of it.
Found The Missing Material Design 3 Carousel
Exploration and strategies for implementing the Material Design carousel.
Missing From Flutter SDK Material Design 3 Focus Ring
The Material Design 3 focus ring is not in the Flutter SDK.
This is how to implement it.
Fixing Material You Flutter Theming
How to implement themes support the dynamic user chosen color in
Flutter using the Flutter SDK ColorScheme and ThemeData way of doing
it.
Material You Themes Superpwoered With FlexColorScheme
A better way to fill those component themes with dynamic color magic
Missing In Flutter SDK Color Scheme Tone Themes
Material Color Utilities tone variations are not in the Flutter SDK Color Schemes yet, but
you can use Flex Color Scheme instead to get them and many more!
Engineering Use Case Canonical Layouts
Mixing Adaptive Scaffold And Canonical Layouts together
An Engineering Challenge Designing A Flutter Adaptive Responsive Scaffold
The flutter SDK does not have this so I have to engineer one
MediaQuery Extensions Is The Tool Out Of The Flutter Canonical Layout Mess
Adaptive scaffold and canonical layouts got paired up in MD3 but no solution in
docs or among Flutter GDEs...I instead had to build the tools..
A Step Towards Adaptive Responsive Apps The Stateful Shell Route
To break down the complex adaptive scaffold responsive layout pattern,
start with the go router stateful shell route first