10 Real RxDart Interview Questions

1. What problem does RxDart solve that normal Dart Streams cannot?

Expected Answer

Dart Streams already support asynchronous event handling, but RxDart adds functional reactive operators that make complex stream transformations easier.

Examples:

Dart StreamRxDart
mapmap
asyncMapflatMap
manual combinationcombineLatest
manual timing logicdebounceTime

Example without RxDart:

Stream<String> search(Stream<String> input) async* {
await for (final value in input) {
if (value.length > 2) {
yield await api.search(value);
}
}
}

With RxDart:

input
.debounceTime(Duration(milliseconds: 400))
.switchMap(api.search)

RxDart makes the code:

  • declarative
  • shorter
  • easier to reason about

2. What is the difference between PublishSubject, BehaviorSubject, and ReplaySubject?

Expected Answer

SubjectBehavior
PublishSubjectemits only new events
BehaviorSubjectemits last value to new subscribers
ReplaySubjectemits entire event history

Example:

final subject = BehaviorSubject<int>();
subject.add(1);subject.listen((v) => print(v)); // prints 1

PublishSubject would not emit 1.

ReplaySubject:

final subject = ReplaySubject<int>();
subject.add(1);
subject.add(2);
subject.listen(print);
// prints 1,2

Senior devs should understand when to use each one.


3. Why can BehaviorSubject cause memory leaks?

Expected Answer

BehaviorSubject stores the last value internally.

If the value is large:

image
large list
API response
UI model

It remains in memory until:

subject.close()

This is why BLoC dispose is critical.

@override
void dispose() {
_subject.close();
}

Without closing:

StreamController leak
Memory leak
Widget rebuild leak

4. When should you use switchMap instead of flatMap?

switchMap

Cancels the previous stream when a new event arrives.

Perfect for:

search
network requests
typeahead

Example:

searchText
.debounceTime(Duration(milliseconds: 400))
.switchMap(api.search)

Typing:

f
fl
flu
flut
flutter

Only latest request survives.


flatMap

Runs all streams simultaneously.

Example:

Upload 5 files

You want all uploads running in parallel.


5. Explain combineLatest and when you would use it

combineLatest merges streams.

Example:

email
password
Rx.combineLatest2(
emailStream,
passwordStream,
(email, password) => email.isNotEmpty && password.length > 6
);

Used in:

login forms
filters
search conditions
dependent UI state

This is very common in production apps.


6. What is the difference between debounceTime and throttleTime?

debounceTime

Waits for silence.

Example:

User typing
input.debounceTime(Duration(milliseconds: 400))

Emits after user stops typing.


throttleTime

Limits events frequency.

Example:

button spam
scroll events
button.throttleTime(Duration(seconds: 1))

Prevents multiple triggers.


7. Why should BLoC expose Streams instead of Subjects?

Bad:

BehaviorSubject<int> counter;

Good:

Stream<int> get counter => _counter.stream;

Why?

Encapsulation.

UI should not be able to:

add()
close()
manipulate stream

Only BLoC controls state.


8. What happens if multiple widgets subscribe to the same StreamController?

Depends on the stream type.

Default:

single-subscription stream

Only one listener allowed.

Error:

Bad state: Stream has already been listened to

Solution:

broadcast()

Example:

StreamController.broadcast()

Or use:

BehaviorSubject

Which is broadcast internally.


9. How would you debug a complex RxDart stream pipeline?

Senior engineers usually add logging.

Example:

stream
.doOnData((v) => print("value: $v"))
.doOnError((e) => print("error: $e"))

or:

RxDebug tools

You can also split pipelines:

Bad:

30 operators chained

Better:

step1
step2
step3

10. When should you NOT use RxDart?

RxDart is powerful but sometimes overkill.

Avoid RxDart when:

simple state
small screens
single API call

Example:

ValueNotifier
ChangeNotifier
Cubit

RxDart is best for:

complex event flows
multiple streams
real-time systems
chat
live feeds
search

Bonus Question

Why did Google move away from RxDart toward Cubit/Bloc?

Because:

RxDart learning curve
debugging difficulty
overengineering small apps

However:

large production apps still use Rx heavily.

Examples:

trading apps
chat apps
streaming apps
real-time dashboards

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Lên đầu trang