generic_bloc_provider 2.1.1 copy "generic_bloc_provider: ^2.1.1" to clipboard
generic_bloc_provider: ^2.1.1 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();
  }
}
22
likes
140
pub points
84%
popularity

Publisher

verified publisherrobertohuertas.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