flutter_bloc_stream 3.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

flutter_bloc_stream #

This package provides two helpers to help reduce boilerplate when working with bloc_stream:

BlocStreamProvider #

This is a simple wrapper around the Provider package that automatically disposes BlocStreams for you.

// Create a provider
BlocStreamProvider(
    create: (context) => CounterBloc(),
    child: myApp,
);

// Consume in a child context.
final counter = BlocStreamProvider.of<CounterBloc>(context);

BlocStreamBuilder #

A simple wrapper around StreamBuilder for building a widget tree when the data changes. If the bloc parameter then it will automatically look for the BLoC using BlocStreamProvider.of:

BlocStreamBuilder<CounterBloc, int>(builder: (context, snapshot) {
    if (snapshot.hasError) return Text("Fail.");
    else if (!snapshot.hasData) return Container();

    return Text("${snapshot.data}");
});

3.0.0 #

  • Update for bloc_stream v3

2.1.0 #

  • Update for provider v4

2.0.0 #

  • Update for bloc_stream 2.x

1.0.0 #

  • Initial version

example/lib/main.dart

import 'package:bloc_stream/bloc_stream.dart';
import 'package:flutter_bloc_stream/flutter_bloc_stream.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

class CounterBloc extends BlocStream<int> {
  @override
  int get initialValue => 0;

  void increment() {
    add(value + 1);
  }

  void decrement() {
    add(value - 1);
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [BlocStreamProvider(create: (_) => CounterBloc())],
      child: BlocStreamBuilder<CounterBloc, int>(
        builder: (context, s) {
          return MaterialApp(
            supportedLocales: const [Locale('en')],
            localizationsDelegates: [
              DefaultMaterialLocalizations.delegate,
              DefaultWidgetsLocalizations.delegate,
              _ExampleLocalizationsDelegate(s.data),
            ],
            home: const MyHomePage(),
          );
        },
      ),
    );
  }
}

class ExampleLocalizations {
  static ExampleLocalizations of(BuildContext context) =>
      Localizations.of<ExampleLocalizations>(context, ExampleLocalizations);

  const ExampleLocalizations(this._count);

  final int _count;

  String get title => 'Tapped $_count times';
}

class _ExampleLocalizationsDelegate
    extends LocalizationsDelegate<ExampleLocalizations> {
  const _ExampleLocalizationsDelegate(this.count);

  final int count;

  @override
  bool isSupported(Locale locale) => locale.languageCode == 'en';

  @override
  Future<ExampleLocalizations> load(Locale locale) =>
      SynchronousFuture(ExampleLocalizations(count));

  @override
  bool shouldReload(_ExampleLocalizationsDelegate old) => old.count != count;
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Title()),
      body: const Center(child: CounterLabel()),
      floatingActionButton: const IncrementCounterButton(),
    );
  }
}

class IncrementCounterButton extends StatelessWidget {
  const IncrementCounterButton({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: () {
        BlocStreamProvider.of<CounterBloc>(context).increment();
      },
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    );
  }
}

class CounterLabel extends StatelessWidget {
  const CounterLabel({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        const Text(
          'You have pushed the button this many times:',
        ),
        BlocStreamBuilder<CounterBloc, int>(
          builder: (context, s) => Text(
            '${s.data}',
            style: Theme.of(context).textTheme.display1,
          ),
        ),
      ],
    );
  }
}

class Title extends StatelessWidget {
  const Title({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(ExampleLocalizations.of(context).title);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_bloc_stream: ^3.0.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_stream/flutter_bloc_stream.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
33
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]
66
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/src/bloc_stream_provider.dart. (-0.50 points)

Analysis of lib/src/bloc_stream_provider.dart reported 1 hint:

line 64 col 31: Avoid using braces in interpolation when not needed.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
bloc_stream >=3.0.0 <4.0.0 3.0.0+1
flutter 0.0.0
provider >=4.0.0 <5.0.0 4.0.4 4.1.0-dev+2
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
nested 0.0.4
rxdart 0.23.1 0.24.0-dev.1
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
pedantic ^1.8.0
test ^1.6.0