Git Product home page Git Product logo

bloc_presentation's Introduction

bloc_presentation

pub.dev badge Build status

Extends blocs with an additional stream which can serve as a way of indicating single-time events (so-called "presentation events").

installation

flutter pub add bloc_presentation

usage

First, create an event which will be emitted:

class FailedToUpvote implements BlocPresentationEvent {
  const FailedToUpvote(this.reason);

  final String reason;
}

Next, extend your Bloc/Cubit with the presentation mixin which will give you access to the emitPresentation method:

class CommentCubit extends Cubit<CommentState> with BlocPresentationMixin {
  // body
}

Now in your methods instead of emitting new state, you can emit a single-time presentation event without overwriting your bloc/cubit state:

void upvote() {
	// upvoting logic

	if (!success) {
		// we can emit it and forget about cleaning it from the state
		emitPresentation(const FailedToUpvote('bad connection'));
	} else {
		emit(/* new state */);
	}
}

In this case above, we do not want to lose our bloc/cubit state after a non-fatal failure. Instead, we want to communicate this failure and not emit any new states. Then, in the UI code one can react to such events using BlocPresentationListener or useBlocPresentationListener:

BlocPresentationListener<CommentCubit>(
	listener: (context, event) {
		if (event is FailedToUpvote) {
			ScaffoldMessenger.of(context)
				..hideCurrentSnackBar()
				..showSnackBar(SnackBar(content: Text(event.reason)));
		}
	},
	child: MyWidget(),
)

By default, CommentCubit will be looked up using package:provider in the widget tree. However, a bloc can be provided directly using the BlocPresentationListener.bloc parameter (analogous to how package:bloc listeners work).

example

See example

bloc_presentation's People

Contributors

shilangyu avatar mateuszwojtczak avatar albert221 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.