Code Monkey home page Code Monkey logo

notifier_bloc's Introduction

Extended BLoC with ability to send notifications in separate stream

ezgif-3-be389728c9

Features

  • Execute side effects like navigation/toasts/notifications etc..
  • Show error messages

Usage

1. Create Bloc

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:notifier_bloc/notifier_bloc.dart';

part 'counter_event.dart';
part 'counter_state.dart';
part 'counter_notification.dart';

class CounterBloc extends NotifierBloc<CounterEvent, CounterState, CounterNotification> {
  CounterBloc() : super(const CounterState(0)) {
    on<Increment>(_onIncrement);
    on<Decrement>(_onDecrement);
    on<Reset>(_onReset);
  }

  FutureOr<void> _onIncrement(Increment event, Emitter<CounterState> emit) {
    emit(CounterState(state.value + 1));
  }

  FutureOr<void> _onDecrement(Decrement event, Emitter<CounterState> emit) {
    emit(CounterState(state.value - 1));
    emitNotification(DecrementedNotification());
  }

  FutureOr<void> _onReset(Reset event, Emitter<CounterState> emit) {
    emit(const CounterState(0));
    emitNotification(ResetNotification());
  }
}

2. Emit notification

emitNotification(IncrementedNotification());

3. Listen on notifications

NotifierBlocListener<CounterBloc, CounterNotification>(
    onNotification: (context, notification) {
      final message = switch (notification) {
        IncrementedNotification() => 'Counter Incremented',
        DecrementedNotification() => 'Counter Decremented',
        ResetNotification() => 'Counter reset completed'
      };

      final messenger = ScaffoldMessenger.of(context);

      messenger.hideCurrentSnackBar();

      messenger.showSnackBar(
        SnackBar(
          content: Text(message),
        ),
      );
    },
)

notifier_bloc's People

Contributors

s-deer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.