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),
      ),
    );
  }
}
Available extensions

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
hasValue bool
Returns true if the subject has a current value.
no setter
isClosed bool
Whether the underlying subject is closed.
no setter
isDisposed bool
Checks if the subject has been disposed
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
valueOrNull → T?
Returns the current value if available, otherwise returns null. This is a safe alternative to value that doesn't throw exceptions.
no setter

Methods

add(T value) → void
Adds a new value to the subject with disposal check
addError(Object error, [StackTrace? stackTrace]) → void
Adds an error to the subject.
buffer(Stream closing) ReactiveSubject<List<T>>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTimeControlExtension extension

Buffers values from the source ReactiveSubject until the closing stream emits.
cache() ReactiveSubject<T>
Caches the latest value and replays it to new subscribers. This is more memory efficient than shareReplay for single value caching.
debounceTime(Duration duration) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTimeControlExtension extension

Emits items from the source ReactiveSubject only after a specified duration has passed without the ReactiveSubject emitting any other items.
debug({String? tag, void onValue(T value)?, void onError(Object error)?}) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Adds debugging capabilities to the ReactiveSubject by logging events.
dispose() Future<void>
Disposes the subject and prevents further usage
distinct([bool equals(T previous, T next)?]) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectStateManagementExtension extension

Emits all items emitted by the source ReactiveSubject that are distinct from their immediate predecessors.
distinctBy<K>(K keySelector(T value)) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectStateManagementExtension extension

Emits items that are distinct based on a key selector function. This is more efficient than distinct() when you only need to compare specific properties.
doOnData(void onData(T event)) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Performs a side-effect action for each data event emitted by the source ReactiveSubject.
doOnError(void onError(Object error, StackTrace stackTrace)) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Performs a side-effect action for each error event emitted by the source ReactiveSubject.
groupBy<K>(K keySelector(T value)) ReactiveSubject<Map<K, List<T>>>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectStateManagementExtension extension

Groups stream events into separate lists based on a key selector function.
listen(void onData(T value), {Function? onDone, Function? onError, bool? cancelOnError}) StreamSubscription<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Listens to the stream and calls the provided callbacks.
listenManaged(void onData(T value), {Function? onDone, Function? onError, bool? cancelOnError}) StreamSubscription<T>
Creates a managed subscription that will be automatically cancelled on dispose
map<R>(R mapper(T event)) ReactiveSubject<R>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTransformationsExtension extension

Transforms the items emitted by the source ReactiveSubject by applying a function to each item.
mapNotNull<R>(R? mapper(T event)) ReactiveSubject<R>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTransformationsExtension extension

Transforms each item emitted by the source ReactiveSubject by applying a function that returns a nullable value, and only emits non-null results.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onErrorResumeNext(ReactiveSubject<T> recoveryFn(Object error)) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectErrorHandlingExtension extension

Catches errors from the source ReactiveSubject and executes a recovery function to continue the stream.
pairwise() ReactiveSubject<List<T>>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Emits the previous and current values as a pair.
retry([int? count]) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectErrorHandlingExtension extension

Retries the source ReactiveSubject when an error occurs.
retryWhen(Stream<void> retryWhenFactory(Stream<Object>)) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectErrorHandlingExtension extension

Retries the source ReactiveSubject when an error occurs, with a delay between retries.
scan<R>(R initialValue, R accumulator(R accumulated, T current, int index)) ReactiveSubject<R>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectCombinationsExtension extension

Applies an accumulator function over the source ReactiveSubject, and returns each intermediate result as a ReactiveSubject.
share() ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectStateManagementExtension extension

Shares a single subscription to the source ReactiveSubject among multiple subscribers.
shareReplay({int maxSize = 1}) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectStateManagementExtension extension

Shares a single subscription and replays the specified number of latest values to new subscribers.
skipLast(int count) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Skips the last count items emitted by the source ReactiveSubject.
startWith(T startValue) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectCombinationsExtension extension

Prepends a given value to the source ReactiveSubject.
switchMap<R>(ReactiveSubject<R> mapper(T event)) ReactiveSubject<R>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTransformationsExtension extension

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.
takeLast(int count) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Takes the last count items emitted by the source ReactiveSubject.
takeWhileInclusive(bool test(T event)) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Emits items from the source ReactiveSubject while the test function returns true. Unlike Stream.takeWhile, this includes the first item that fails the test.
throttleTime(Duration duration) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTimeControlExtension extension

Emits the first item emitted by the source ReactiveSubject in each time window of a specified duration.
timeInterval() ReactiveSubject<TimeInterval<T>>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Emits the time interval between consecutive emissions.
timestamp() ReactiveSubject<Timestamped<T>>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectUtilitiesExtension extension

Emits each item with a timestamp indicating when it was emitted.
toString() String
A string representation of this object.
inherited
valueOr(T defaultValue) → T
Returns the current value if available, otherwise returns the provided default value.
where(bool test(T event)) ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTransformationsExtension extension

Filters the items emitted by the source ReactiveSubject by only emitting those that satisfy a specified predicate.
whereNotNull() ReactiveSubject<T>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectTransformationsExtension extension

Emits only items that are not null from the source ReactiveSubject.
withLatestFrom<S, R>(ReactiveSubject<S> other, R combiner(T event, S latestFromOther)) ReactiveSubject<R>

Available on ReactiveSubject<T>, provided by the ReactiveSubjectCombinationsExtension extension

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<T>> subjects) ReactiveSubject<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()?, Duration? timeout}) ReactiveSubject<T>
Creates a ReactiveSubject from a Future, with error handling and completion callback.
merge<T>(List<ReactiveSubject<T>> subjects) ReactiveSubject<T>
Merges multiple ReactiveSubjects into a single ReactiveSubject.