Skip to content

Track a subject

rxdart Subjects (BehaviorSubject, PublishSubject, ReplaySubject) get richer treatment than plain Streams in rxdart_devtools — beyond appearing in the panel, a tracked Subject can also be driven from DevTools via the injection API.

Quick example

Wrap the Subject at the point you create it. .track('name').asSubject() returns the original Subject so the rest of your code is unchanged:

import 'package:rxdart/rxdart.dart';
import 'package:rxdart_devtools/sdk.dart';
final counter = BehaviorSubject<int>.seeded(0)
.track('counter')
.asSubject();

The Subject appears in the DevTools panel labelled counter. Every value passed to counter.add(...) shows up in the Current value card and the Event log.

How it differs from Stream.track()

.track() is also available on plain Streams — it returns a TrackedStream<T> and you call .asStream() to unwrap. For Subjects you should prefer the Subject-typed version because it:

  • Preserves the Subject subtype. BehaviorSubject<int>.seeded(0).track('counter').asSubject() returns a BehaviorSubject<int>, not a Subject<int> — so counter.value, counter.add(...), and the rest of the BehaviorSubject API stay accessible.
  • Unlocks injection. Only TrackedSubject exposes enableInjection(...).

Push values from the panel (enableInjection)

If you want the panel to be able to push values into the Subject (handy for triggering edge cases without rebuilding the app), chain .enableInjection(parse:):

final counter = BehaviorSubject<int>.seeded(0)
.track('counter')
.enableInjection(parse: int.tryParse)
.asSubject();

parse converts the raw text from the panel’s input field into your Subject’s value type. It must return T? — returning null causes the panel to display a parse error and leaves the Subject unchanged.

Common parsers:

Subject<T>parse
Subject<int>int.tryParse
Subject<double>double.tryParse
Subject<num>num.tryParse
Subject<bool>bool.tryParse
Subject<String>(raw) => raw
Custom domain typeMyType.tryParse or a factory function

In the panel, the inject dialog has a Value / Error toggle:

  • Value — runs parse(rawText) then subject.add(...).
  • Error — calls subject.addError(rawText) with the typed string. Useful for exercising error-handling code paths without changing your app source.