π Stream Value
Stream Value is a simple and lightweight state management solution for Flutter that wraps StreamController and StreamBuilder to eliminate boilerplate.
Unlike other packages, stream_value is focused purely on state. It does not include dependency injectionβstaying true to the single responsibility principle.
β¨ Features
- β Minimal Boilerplate β Use streams with ease
- π― Focused State Management β No DI, just state
- β‘ Lightweight & Simple β Easy to learn and integrate
- π‘οΈ Null-Safety by Default β Handles nullable & non-nullable types
- π§© Built-in Error Handling β Show error widgets with ease
π Getting Started
π¦ Installation
Add to your pubspec.yaml:
dependencies:
stream_value: ^0.0.5 # Check for the latest version
Then run:
flutter pub get
π‘ Usage
The package provides two main classes:
StreamValue
StreamValueBuilder
β 1. Create a StreamValue
final nameStream = StreamValue<String?>(); // Nullable
final counterStream = StreamValue<int>(defaultValue: 0); // Non-nullable
π 2. Update the Value
void updateName(String newName) {
nameStream.addValue(newName);
}
void incrementCounter() {
final currentCount = counterStream.value;
counterStream.addValue(currentCount + 1);
}
π§± 3. Build Your UI
class CounterPage extends StatelessWidget {
final counterStream = StreamValue<int>(defaultValue: 0);
CounterPage({super.key});
void _increment() {
counterStream.addValue(counterStream.value + 1);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('StreamValue Example')),
body: Center(
child: StreamValueBuilder<int>(
streamValue: counterStream,
builder: (context, value) {
return Text(
'You have pushed the button this many times: $value',
style: Theme.of(context).textTheme.headlineMedium,
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _increment,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
β Handling Null & Error States
StreamValueBuilder<User?>(
streamValue: userStream,
onNullWidget: const Center(child: CircularProgressIndicator()),
onErrorBuilder: (error) => Center(child: Text('Error: $error')),
builder: (context, user) {
return Text('Hello, ${user.name}');
},
),
π§ͺ Verified Source Details
β StreamValue
-
Uses StreamController
-
Supports null safety and default values
-
Provides .value and .error access
-
Handles manual error injection via addError(...)
-
.dispose() safely closes both controllers
β StreamValueBuilder
-
Rebuilds using StreamBuilder
-
Supports:
-
onNullWidget
-
onErrorBuilder
-
Default builder
-
Works with nullable and non-nullable streams
π€ Contributing
Contributions are welcome! This package focuses on simplicity. Please keep PRs in line with the existing lightweight architecture.
-
Fork it π΄
-
Create your feature branch π
-
Push & open a PR β
-
Before adding a major feature, open an issue to discuss it.
π License
MIT License. See the LICENSE file for full details.