GithubHelp home page GithubHelp logo

robertohuertasm / generic_bloc_provider Goto Github PK

View Code? Open in Web Editor NEW
27.0 2.0 6.0 28 KB

Generic BloC provider implementation

Home Page: https://pub.dartlang.org/packages/generic_bloc_provider

License: MIT License

Dart 100.00%
flutter bloc state-management

generic_bloc_provider's Introduction

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;

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.