Skip to content

A Voyager plugin for the felangel/bloc library

License

Notifications You must be signed in to change notification settings

eyeem/voyager_bloc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

voyager_bloc

pub package Codemagic build astatus codecov

Adds ability to specify used BLoCs on the widget level.

Usage

Specify wanted blocs per screen in YAML file, like so:

'/my/fancy/path':
  widget: FancyWidget
  blocs:
    - BlocA : 12 # provide config for bloc after :
    - BlocB :
        - field1: "hello"
        - field2: "world"
    - BlocC@foo : "enemy" # use @ to provide named blocs
    - BlocC@bar : "friend"

Use BlocPluginBuilder to provide mappings for your blocs:

BlocPluginBuilder()
  .addBaseBloc<BlocA>((routeContext, config, repository) => /* return BlocA here */)
  .build()

where repository gives you access to other blocs from your the scope of your page.

Class Hierarchy & Bloc Plugin Builder

Flutter Dart has no reflection and runtimeType doesn't contain information about parent classes, therefore voyager bloc plugin builder has specific API to address this issue:

If your bloc class (e.g. ParentBloc) is extending directly from Bloc use:

addBaseBloc<ParentBloc>((routeContext, config, repository) {
    return ParentBloc();
})

If your bloc doesn't extend directly from Bloc but e.g. from ParentBloc you will want to use:

addBloc<ChildBloc, ParentBloc>((routeContext, config, repository) {
    return ChildBloc();
})

Schema Validator

If you're using schema validation with voyager:codegen you can add the following to cover basics

blocs:
  output: BlocRepository
  import: 'package:voyager_bloc/voyager_bloc.dart'
  input:
    type: array

Accessing Blocs

Once you are working in the buildContext of Widget you can obtain BlocRepository

final repo = Provider.of<Voyager>(context)["blocs"];

or if you use generated strong types:

final repo = VoyagerProvider.of(context).blocs;

From there you can find blocs by type, e.g.:

final blocA = repo.find<BlocA>();

...and if your bloc was given a specific name, then supply name parameter:

final fooBlocC = repo.find<BlocC>(name: "foo");

About

A Voyager plugin for the felangel/bloc library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 96.7%
  • Swift 2.4%
  • Other 0.9%