generic_bloc_provider 1.1.0
generic_bloc_provider: ^1.1.0 copied to clipboard

A generic BloC Provider for your Flutter apps. This package will help you avoid the boilerplate of writing BloC Providers.

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: Column(
          children: [
            StreamBuilder(
              stream: appBloc.text$,
              builder: (context, AsyncSnapshot<String> snapshot) {
                return Text(
                    snapshot.hasData ? snapshot.data ?? 'no data' : 'loading');
              },
            ),
            FlatButton(
              onPressed: () {
                var time = DateTime.now().toUtc().toIso8601String();
                appBloc.text.add('Pressed at $time');
              },
              child: Text('PRESS THIS'),
            )
          ],
        ),
      ),
    );
  }
}

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

  void dispose() {
    _text$.close();
  }
}
16
likes
110
pub points
88%
popularity

Publisher

robertohuertas.com

A generic BloC Provider for your Flutter apps. This package will help you avoid the boilerplate of writing BloC Providers.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on generic_bloc_provider