https://github.com/fmmalta/bloc_generic_streams
A Flutter package to implement Didier Boelens BlocProvider and simplified use of Stream using Dart's Generics
https://github.com/fmmalta/bloc_generic_streams
bloc dart flutter management reactive rxdart state streams
Last synced: 5 months ago
JSON representation
A Flutter package to implement Didier Boelens BlocProvider and simplified use of Stream using Dart's Generics
- Host: GitHub
- URL: https://github.com/fmmalta/bloc_generic_streams
- Owner: fmmalta
- License: mit
- Created: 2020-02-26T06:04:37.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-08T12:13:15.000Z (over 4 years ago)
- Last Synced: 2024-11-06T06:52:04.881Z (11 months ago)
- Topics: bloc, dart, flutter, management, reactive, rxdart, state, streams
- Language: Dart
- Homepage: https://pub.dev/packages/bloc_generic_streams
- Size: 101 KB
- Stars: 1
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Bloc Generic Streams
This package was create to be an easy implementation of Didier Boelens's BlocProvider and including a new way to use a BehaviorSubject using GenericStream class that creates an object more simple with Dart's Generics.
I built this package because when you have an application like an Uber application, which was my case, others bloc packages didn't attended my expectations, so, I created this, more simple and more intuitive. You only need to create your Bloc extending BlocBase and then creating a BlocProvider in whatever screen you desire.
## Example:
```dart
import 'package:bloc_generic_streams/bloc_generic_streams.dart';
class HomeBloc extends BlocBase {
int counter = 0;
final GenericBehavior counterStream = GenericBehavior();final GenericPublish publishSubject = GenericPublish();
final GenericReplay publishSubject = GenericReplay();
void increment() {
counterStream.sink(++counter);
}
@override
void dispose() {
counterStream?.dispose();
}
}
```counterStream variable contains all most important methods exposed, like:
- sink (StreamSink)
- stream (ValueStream)
- stream debounce (1500 ms)## How to use BlocProvider?
```dart
class HomeScreen extends StatelessWidget {
//Instantiate our bloc... HomeBloc in our case
final HomeBloc _bloc = HomeBloc();
@override
Widget build(BuildContext context) {
//Return a BlocProvider instance specifying its type
return BlocProvider(
blocBuilder: () => _bloc, // Build and create our bloc
child: Scaffold(
appBar: AppBar(title: Text('Home Screen')),
floatingActionButton: FloatingActionButton(
onPressed: _bloc.increment,
child: Icon(Icons.add),
),
body: Center(
child: StreamBuilder(
initialData: _bloc.counter,
//Retrieving Stream from _bloc's GenericStream object
stream: _bloc.counterStream.stream,
builder: (_, AsyncSnapshot snapshot) {
return Text(
'${snapshot.data}',
style: Theme.of(context).textTheme.display1,
);
},
),
),
),
);
}
}
```
Now it's very easy to implement a BLoC class and to manipulate the way you want.## Retrieving Bloc
Follow the same pattern from other packages, like this:
```dart
final HomeBloc _bloc = BlocProvider.of(context);
```With that, you can access and retrieved any stream/method inside your Bloc.