ReactiveSubject<T> class
A wrapper class for RxDart subjects that provides a simplified interface for reactive programming.
This class encapsulates either a BehaviorSubject or a PublishSubject and provides methods to interact with the underlying subject in a more convenient way.
Usage:
// Create a ReactiveSubject with an initial value
final subject = ReactiveSubject<int>(initialValue: 0);
// Add a value
subject.add(1);
// Listen to the stream
subject.stream.listen((value) => print(value));
// Get the current value
print(subject.value);
// Dispose when done
subject.dispose();
Usage with StreamBuilder in a widget:
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
final ReactiveSubject<int> _counter = ReactiveSubject<int>(initialValue: 0);
@override
void dispose() {
_counter.dispose();
super.dispose();
}
void _incrementCounter() {
_counter.add(_counter.value + 1);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: StreamBuilder<int>(
stream: _counter.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text('Counter: ${snapshot.data}');
} else {
return CircularProgressIndicator();
}
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
child: Icon(Icons.add),
),
);
}
}
Constructors
- ReactiveSubject({T? initialValue})
- Creates a ReactiveSubject with a BehaviorSubject.
- ReactiveSubject.broadcast({T? initialValue})
- Creates a ReactiveSubject with a PublishSubject.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isClosed → bool
-
Whether the underlying subject is closed.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
sink
→ Sink<
T> -
The sink of the underlying subject.
no setter
-
stream
→ Stream<
T> -
The stream of the underlying subject.
no setter
- value → T
-
The current value of the subject.
no setter
Methods
-
add(
T value) → void - Adds a new value to the subject.
-
addError(
Object error, [StackTrace? stackTrace]) → void - Adds an error to the subject.
-
debounceTime(
Duration duration) → ReactiveSubject< T> - Emits items from the source ReactiveSubject only after a specified duration has passed without the ReactiveSubject emitting any other items.
-
dispose(
) → Future< void> - Closes the underlying subject.
-
distinct(
[bool equals(T previous, T next)?]) → ReactiveSubject< T> - Emits all items emitted by the source ReactiveSubject that are distinct from their immediate predecessors.
-
doOnData(
void onData(T event)) → ReactiveSubject< T> - Performs a side-effect action for each data event emitted by the source ReactiveSubject.
-
doOnError(
void onError(Object error, StackTrace stackTrace)) → ReactiveSubject< T> - Performs a side-effect action for each error event emitted by the source ReactiveSubject.
-
map<
R> (R mapper(T event)) → ReactiveSubject< R> - Transforms the items emitted by the source ReactiveSubject by applying a function to each item.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
scan<
R> (R initialValue, R accumulator(R accumulated, T current, int index)) → ReactiveSubject< R> - Applies an accumulator function over the source ReactiveSubject, and returns each intermediate result as a ReactiveSubject.
-
startWith(
T startValue) → ReactiveSubject< T> - Prepends a given value to the source ReactiveSubject.
-
switchMap<
R> (ReactiveSubject< R> mapper(T event)) → ReactiveSubject<R> - Transforms the items emitted by the source ReactiveSubject by applying a function that returns a ReactiveSubject, then emitting the items emitted by the most recently created ReactiveSubject.
-
throttleTime(
Duration duration) → ReactiveSubject< T> - Emits the first item emitted by the source ReactiveSubject in each time window of a specified duration.
-
toString(
) → String -
A string representation of this object.
inherited
-
where(
bool test(T event)) → ReactiveSubject< T> - Filters the items emitted by the source ReactiveSubject by only emitting those that satisfy a specified predicate.
-
withLatestFrom<
S, R> (ReactiveSubject< S> other, R combiner(T event, S latestFromOther)) → ReactiveSubject<R> - Combines the latest values of two ReactiveSubjects using a specified combiner function.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
combineLatest<
T> (List< ReactiveSubject< subjects) → ReactiveSubject<T> >List< T> > - Combines the latest values of multiple ReactiveSubjects into a single ReactiveSubject that emits a List of those values.
-
fromFutureWithError<
T> (Future< T> future, {dynamic onError(Object error)?, dynamic onFinally()?}) → ReactiveSubject<T> - Creates a ReactiveSubject from a Future, with error handling and completion callback.
-
merge<
T> (List< ReactiveSubject< subjects) → ReactiveSubject<T> >T> - Merges multiple ReactiveSubjects into a single ReactiveSubject.