flutter_bloc_provider 0.6.0

flutter_bloc_provider #

This package is part of a number of packages designed to help create BLoC architected code in Dart and Flutter. You can view them all in the repository here.

This README relies on the fact that you have read the main README.

Installation #

dependencies:
  flutter_bloc_provider: <latest_version>

Provider #

A BLoCProvider<BLoC> is an InheritedWidget that allows the BLoC to be accessible to sub widgets. Calling BLoCProvider<BLoC>.of(context) will return the BLoC from the tree.

Make sure you call the dispose method of the BLoCProvider when you are done with it to close any open streams and stop any services.

Disposer #

A BLoCDisposer<BLoC> is a StatefulWidget that wraps a child in a BLoCProvider. The disposer will automatically call the dispose method on the provider when it loses context.

Multi Providers and Disposers #

The BLoCMultiProvider and BLoCMultiDisposer allow multiple BLoCs to be added to the widget tree at once and behave exactly as their singular counterparts.

Changelog #

0.6.0 #

  • Require services to be initialized by the template
  • Allow BLoCValue to have initial data
  • Replace individual service require with generic
  • Move BLoCParamater to the constructor
  • Overhaul bloc_generator

0.5.1 #

  • Add BLoCMultiProvider and BLoCMultiDisposer

0.5.0 #

  • Add Documentation
  • Refactor
  • Format
  • Add CI
  • Make parameters private by default

0.4.4 #

  • Changed name from flutter_bloc_generator to bloc_generator

0.3.0 #

  • Removed AsyncMapperService
  • Made all mappers streams
  • Split out providers and disposer to flutter_bloc_provider package

0.2.6 #

  • Add MapperService, AsyncMapperService, RequireMapperService and RequireAsyncMapperService

0.2.5 #

  • Currect trigger types to Future
  • Make mappers optionally async

0.2.4 #

  • Add TriggeredService

0.2.3 #

  • Allow mappers to return null to not add anything to the output

0.2.2 #

  • Added parameters that can be passed to the BLoC and are accessible to BLoC services

0.2.1 #

  • Make requiring a service require the service type
  • Add BLoCService service that takes an entire BLoC instead of a Stream or Sink

0.2.0 #

  • Make mappers async

0.1.9 #

  • Make template value the only current value stored

0.1.8 #

  • Moved value updater before calling mappers
  • Updating latest values on template

0.1.7 #

  • OutputService automatic listener
  • OutputService automatic subscription disposer

0.1.6 #

  • Remove current data from mappers

0.1.5 #

  • Made services public members

0.1.4 #

  • Changed Service to InputService and added OutputService

0.1.3 #

  • Format code
  • Add example

0.1.2 #

  • Fix latest value updater for initial data

0.1.1 #

  • Change analyzer dependency to ^0.33.0 for compatability

0.1.0 #

  • Initial public release

example/lib/main.dart

// Copyright 2019 Callum Iddon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:flutter/material.dart';

import 'package:flutter_bloc_provider/flutter_bloc_provider.dart';

import 'package:flutter_bloc_example/bloc.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(final BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BLoCDisposer<TestBLoC>(
        bloc: TestBLoC(maxValue: 20),
        child: const MyHomePage(title: 'Flutter bloc_generator Demo'),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  const MyHomePage({this.title});

  @override
  Widget build(final BuildContext context) {
    final TestBLoC bloc = BLoCProvider.of<TestBLoC>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
            StreamBuilder<String>(
              stream: bloc.counter,
              builder: (final BuildContext context,
                  final AsyncSnapshot<String> snapshot) {
                if (!snapshot.hasData) {
                  return Container();
                }

                return Column(children: <Widget>[
                  Text(
                    snapshot.data,
                    style: Theme.of(context).textTheme.display1,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      MaterialButton(
                        child: const Text('Reset Counter'),
                        onPressed: () => bloc.setCounter.add(0),
                      ),
                      MaterialButton(
                        child: const Text('Trigger Url Service'),
                        onPressed: () {
                          if (int.parse(bloc.currentCounter) != 0) {
                            bloc.urlService.trigger();
                          } else {
                            print('Hit the + button first.');
                          }
                        },
                      ),
                    ],
                  ),
                ]);
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => bloc.addToCounter.add(1),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_bloc_provider: ^0.6.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_bloc_provider/flutter_bloc_provider.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
56
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]
78
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
bloc_annotations ^0.6.0 0.6.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

Admin