Flutter package for the Model-Widget-BLoC (MWB) pattern.
This project contains a small set of classes and a sample project to demonstrate the MWB pattern, which is inspired by the Reactive Programming - Streams - BLoC article.
The actual library code is fairly small and only contains a few classes. The reason for this is that the MWB pattern shown in this repository is trying to keep things small and simple, but rich on possibilities to extend while being less dependant on libraries that dictate structure.
For further explanation of the pattern read the README of the sample project
General structure #
The idea is the same as with other state-management patterns: Separate the "responsability & ownership" for logic & data from UI classes.
- Widget: Plain UI, straightforward Widgets that display something
- BLoC: Business Logic Component. An abstract object that contains cached data/state and can execute business logic. This is where the access to the model classes happens.
- Model: Anything that can act as a data source. That can be a DAO, or a class directing to REST endpoints or similar.
Library contents #
The library itself contains 3 classes: BlocProvider, BaseBloc, Instantiable.
Instantiables are objects with a lifecycle, similar to the one of a widget State. So every time you need an initialization and a destruction process you can implement this class.
BaseBlocs are derived from Instantiables. Their primary purpose is to mark classes explicitly as a Bloc which are used by the BlocProvider.
BlocProviders are widgets that are used to hold the instance of a bloc, manage it's lifecycle and provide that instance down the widget tree.
Bloc access #
In order to create a Bloc for a "widget tree scope" you'll have to wrap that widget inside a
BlocProvider( bloc: MyPageBloc(), child: MyPage(), );
You can then access the bloc instance by calling
_bloc = BlocProvider.of(context)
inside your widget/state (usually in
initState). Casting is performed automatically.
* Moved sample project into separate folder * Removed redundant initialization parameter from Instantiable class * Updated documentation
* Initial release * Setup sample application
Use this package as a library
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies: mwb: ^1.0.1
2. Install it
You can install packages from the command line:
$ flutter pub get
Alternatively, your editor might support
flutter pub get.
Check the docs for your editor to learn more.
3. Import it
Now in your Dart code, you can use:
import 'package:mwb/bloc_provider.dart'; import 'package:mwb/instantiable.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
- Dart: 2.8.4
- pana: 0.13.14
- Flutter: 1.17.5
Package not compatible with SDK dart
mwbthat is a package requiring null.
The package description is too short. (-11 points)
Add more detail to the
description field of
pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.
Maintain an example.
None of the files in the package's
example/ directory matches known example patterns.
Common filename patterns include
mwb.dart. Packages with multiple examples should provide
For more information see the pub package layout conventions.