context_provider 0.1.0

  • Readme
  • Changelog
  • Installing
  • 48

context_provider #

Flutter context provider to provide state with reducer combined as map of type and the reducer.
along with the useSelector hook and useDispatch hook

Usage #

this package contains of

  - ContextProvider Widget
  - useSelector function
  - useDispatch function
  - Reducer abstract class

ContextProvider widget #

Use it with following prop
Map<Type extends Reducer, Reducer> reducer and Widget child\

  ContextProvider(reducer: myReducer, child: MyWidget());

the Type is implemented class from Reducer abstract class provided.

useSelector function #

Use this inside your widget to get the reducer/state data from the state.

final T myState = useSelector<T extends Reducer>(context)

where T is implementation of the Reducer abstract class.

You can pass another parameter if you decide to listen to the state changes.

final T myState = useSelector<T extends Reducer>(context, listen: false)

it's true by default.

useDispatch function #

final Function dispatch = useDispatch(context);

which dispatch function takes arguments of Map<String, dynamic> that had type key in the action.

you can pass another parameter if you want to listen to changes.

final Function dispatch = useDispatch(context, listen: true);

it's false by default.

Reducer abstract class #

Here's where you implement the state shape/what the state holds in the instance of the reducer.\

Full reducer example #

import 'package:context_provider/context_provider.dart';

class ProductReducer extends Reducer {
  Map<int, Map<String, dynamic>> _product = {};

  get product => this._product;

  void dispatch(Map<String, dynamic> action) async {
    switch (action["type"]) {
      case SOME_ACTION:
        //  do something here
        super.notifyListeners();  // notify widgets that listens to this reducer state
      case INIT:
        // do something else here

You want to @override the dispatch method inside the class and implement your own reducer to handle actions that'll be handled by the reducer instance.

void dispatch(Map<String, dynamic> action) async {
  switch (action["type"]) {
    case "DO_SOMETHING":
    // do something with this

methods that were available from this class is following\

  - notifyListeners 

Which notifyListeners is used whenever you want to notify the consumer of your state to re-render.

[0.0.1] - First Release: 3rd April 2020. #

First release, features:

  • ContextProvider widget
  • useSelector & useReducer function
  • Reducer abstract class

[0.0.2] - Updated dependencies #

  • Provider to 4.0.5

[0.0.3] - Updated description #

  • Updated description for this package

[0.1.0] - Remove in reducer dispatch & share state #

  • Remove in-reducer dispatch
  • Remove in-reducer state selector

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  context_provider: ^0.1.0

2. Install it

You can install packages from the command line:

with Flutter:

$ 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:context_provider/context_provider.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]
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [context_provider] that is in a package requiring null.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and context_provider.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
provider ^4.0.5 4.3.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
nested 0.0.4
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies