Search packages, repositories and app categories

Package detailsv1.0.8

dash

Dash library is responsible to offer a service locator pattern that cares about provide and dispose blocs.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
182
Likes
10
Pub points
160

Install package

flutter pub add dash

Package Overview

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

Dash is a lightweight service locator package for Flutter that simplifies dependency injection by managing the lifecycle of blocs, ensuring proper instantiation and disposal. It provides a clean way to register and retrieve dependencies, particularly suited for state management with Bloc patterns. The library supports all major platforms and integrates seamlessly into existing Flutter projects. Its focus on lifecycle awareness helps prevent memory leaks and improves app performance.

Use cases

  • Managing Bloc lifecycle in Flutter apps
  • Simplifying dependency injection
  • Replacing manual Bloc creation and disposal
  • Integrating with state management patterns
  • Streamlining test setup
  • Enabling modular app architecture

Key features

  • Automatic bloc disposal
  • Simple registration and retrieval
  • Platform-agnostic design
  • Built-in lifecycle management
  • Minimal boilerplate
  • Supports nested dependencies

Suitable for

  • Flutter developers using Bloc pattern
  • Apps requiring structured dependency management
  • Teams seeking lightweight DI solutions
  • Projects aiming for clean state management
  • Developers wanting automatic resource cleanup

Considerations

  • Limited to Bloc-focused use cases
  • Requires understanding of service locator pattern
  • Not suitable for complex DI needs
  • May need additional setup for advanced scenarios
  • Less feature-rich than full DI frameworks

Original README

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

Dash

Logo

Dash is a simple but efficient library that helps provide BloCs without headaches.

IMPORTANT: Dart2 is required to use this package.

Installation

  • Add dash to your pubspec.yaml under the dependencies section:
dependencies:
  dash: ^latest_version
  • Add build_runner and dash_generator to your pubspec.yaml under the dev_dependencies section:
dev_dependencies:
  build_runner: ^2.0.3
  dash_generator: ^latest_version

Usage

1 - Create a provider class that will gather BlocProvider annotation:

IMPORTANT: Do not forget to add: part 'provider.g.dart';.

import 'package:dash/dash.dart';

part 'provider.g.dart';

@BlocProvider.register(MySampleBloc)
@BlocProvider.register(MyOtherBloc)
abstract class Provider {}

2 - Extend BLoCs from Bloc. It will allow you to override dispose method.

IMPORTANT: All blocs must have an instance() function that returns the instance of the bloc as below.

class MySampleBloc extends Bloc {

  @override
  dispose() {
    //close all streams
  }

  static Bloc instance() => MySampleBloc();
  
}

class MyOtherBloc extends Bloc {

  @override
  dispose() {
    //close all streams
  }

  static Bloc instance() => MyOtherBloc();
}

3 - Run build_runner, it will generate the provider.g.dart class that you added as part on your provider class.

On project directory run in terminal:

pub run build_runner build

For flutter:

flutter packages pub run build_runner build

Note: If you encounter a conflict error, please add the --delete-conflicting-outputs argument to your command:

flutter packages pub run build_runner build --delete-conflicting-outputs

4 - Finally you can get your BLoC instance from anywhere, for example:

  @override
  Widget build(BuildContext context) {
    final _bloc = $Provider.of<MyOtherBloc>();

    return Container();
  }

  @override
  void dispose() {
    $Provider.dispose<MyOtherBloc>();
    super.dispose();
  }

Changelog

Please see the Changelog page to know what's recently changed.

Categories

Source repository

Projects using this package

No data available yet.