Search packages, repositories and app categories

Package detailsv2.1.1

generic_bloc_provider

A generic BloC Provider for your Flutter apps. This package will help you avoid the boilerplate of writing BloC Providers.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
561
Likes
22
Pub points
160

Install package

flutter pub add generic_bloc_provider

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A Flutter package that simplifies state management by providing a generic BloC Provider, reducing boilerplate code typically required when setting up and managing BloC instances. It enables developers to easily inject and access BloC classes across the widget tree with type safety and minimal setup. Ideal for apps using the BloC pattern, it streamlines dependency injection and enhances code maintainability.

Use cases

  • Simplifying BloC instantiation in large apps
  • Reducing repetitive provider boilerplate
  • Managing multiple BloC instances efficiently
  • Enabling type-safe access to BloCs
  • Supporting clean separation of concerns

Key features

  • Generic type support for BloC classes
  • Minimal boilerplate code
  • Type-safe provider access
  • Easy integration with existing BloC patterns
  • Automatic lifecycle management

Suitable for

  • Developers using the BloC pattern
  • Projects aiming to reduce repetitive code
  • Apps with multiple state managers
  • Teams prioritizing maintainable state logic
  • Flutter projects on multiple platforms

Considerations

  • Requires understanding of BloC pattern
  • Not suitable for simple state needs
  • May increase complexity for small apps
  • Best used with other state management tools
  • Limited customization compared to manual providers

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

generic_bloc_provider

Build Status License: MIT Pub

A generic BloC Provider for your Flutter apps.

Getting Started

All your BloCs must extend the Bloc abstract class. This means that all of them should have a dispose method that will be executed whenever the life of the BloC comes to an end.

In this method, you should take care of closing all the sinks and resources.

Example of how to use it:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      bloc: AppBloc(),
      child: MaterialApp(
        title: 'Yo Sleep',
        home: MainPage(),
        initialRoute: 'main',
        routes: {
          'main': (context) => MainPage(),
        },
      ),
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appBloc = BlocProvider.of<AppBloc>(context);
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Header(),
            RecordList(appBloc),
          ],
        ),
      ),
    );
  }
}

Attaching your context to the InheritedWidget

The BlocProvider class depends on InheritedWidget. Whenever you want to get your BloC, you can decide wether to attach the context of your widget to the InheritedWidget or not.

In order to control this behavior, the static method of has an optional boolean argument (which is true by default) which determines wether your context will be attached or not.

Basically, if you don't provide it or you just set it to true, dependOnInheritedWidgetOfExactType will be used. If you set it to false then getElementForInheritedWidgetOfExactType will be used instead.

Customizing the update policy

If you want to change the way updateShouldNotify behaves you have the option to provide a custom anonymous function to the BlocProvider constructor.

You will notice that there's an argument called updateShouldNotifyOverride which accepts a function receiving a BloC and the internal InheritedWidget:

bool Function(T bloc, _BlocProvider oldWidget);

This is the default implementation of the updateShouldNotify method:

@override
  bool updateShouldNotify(_BlocProvider oldWidget) =>
      updateShouldNotifyOverride != null
          ? updateShouldNotifyOverride!(bloc, oldWidget)
          : oldWidget.bloc != bloc;

Categories

Source repository

Projects using this package