generic_bloc_provider 1.0.8

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 79

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, inheritFromWidgetOfExactType will be used. If you set it to false then ancestorInheritedElementForWidgetOfExactType 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;

[1.0.7] - 2019-01-20

  • Fix deployment script.

[1.0.7] - 2019-01-20

  • Fix example.

[1.0.3] - 2019-01-20

  • Readme update.

[1.0.2] - 2019-01-19

  • Added example.

[1.0.1] - 2019-01-19

  • Readme update.

[1.0.0] - 2019-01-19

  • Initial release.

example/example.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:generic_bloc_provider/generic_bloc_provider.dart';

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: StreamBuilder(
          stream: appBloc.text$,
          builder: (context, AsyncSnapshot<String> snapshot) {
            return Text(snapshot.hasData ? snapshot.data : 'loading');
          },
        ),
      ),
    );
  }
}

class AppBloc extends Bloc {
  final _text$ = StreamController<String>();
  Stream<String> get text$ => _text$.stream;
  Sink<String> get text => _text$.sink;

  void dispose() {
    _text$.close();
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  generic_bloc_provider: ^1.0.8

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:generic_bloc_provider/generic_bloc_provider.dart';
  
Version Uploaded Documentation Archive
1.0.8 Jan 20, 2019 Go to the documentation of generic_bloc_provider 1.0.8 Download generic_bloc_provider 1.0.8 archive
1.0.7 Jan 20, 2019 Go to the documentation of generic_bloc_provider 1.0.7 Download generic_bloc_provider 1.0.7 archive
1.0.6 Jan 20, 2019 Go to the documentation of generic_bloc_provider 1.0.6 Download generic_bloc_provider 1.0.6 archive
1.0.4 Jan 20, 2019 Go to the documentation of generic_bloc_provider 1.0.4 Download generic_bloc_provider 1.0.4 archive
1.0.3 Jan 20, 2019 Go to the documentation of generic_bloc_provider 1.0.3 Download generic_bloc_provider 1.0.3 archive
1.0.2 Jan 20, 2019 Go to the documentation of generic_bloc_provider 1.0.2 Download generic_bloc_provider 1.0.2 archive
1.0.1 Jan 19, 2019 Go to the documentation of generic_bloc_provider 1.0.1 Download generic_bloc_provider 1.0.1 archive
1.0.0 Jan 19, 2019 Go to the documentation of generic_bloc_provider 1.0.0 Download generic_bloc_provider 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
58
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1
  • Flutter: 1.2.1

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test