Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BehaviorSubject.isEmpty never finishes when stream is empty #653

Open
orestesgaolin opened this issue Dec 1, 2021 · 2 comments
Open

BehaviorSubject.isEmpty never finishes when stream is empty #653

orestesgaolin opened this issue Dec 1, 2021 · 2 comments
Labels
waiting for response Waiting for follow up

Comments

@orestesgaolin
Copy link

Consider this case:

  test('test', () async {
    final sub = BehaviorSubject<int>();
    final empty = await sub.isEmpty;
    print(empty);
  });

When run it never finishes.

CleanShot 2021-12-01 at 11 09 03@2x

However, when using .hasValue it works just fine:

  test('test', () async {
    final sub = BehaviorSubject<int>();
    final hasValue = await sub.hasValue;
    print(hasValue);
  });

I wonder if this is a known/expected behavior or bug.

@frankpepermans
Copy link
Member

I think you just need to close that BehaviorSubject

@hoc081098
Copy link
Collaborator

Stream.isEmpty completes when the Stream emits the first value or done event.

Future<bool> get isEmpty {
  _Future<bool> future = new _Future<bool>();
  StreamSubscription<T> subscription =
      this.listen(null, onError: future._completeError, onDone: () {
    future._complete(true);
  }, cancelOnError: true);
  subscription.onData((_) {
    _cancelAndValue(subscription, future, false);
  });
  return future;
}

Fix:

 test('test', () async {
    final sub = BehaviorSubject<int>();
    sub.close();
    final empty = await sub.isEmpty;
    print(empty);
  });

 test('test', () async {
    final sub = BehaviorSubject<int>();
    sub.add(1);
    final empty = await sub.isEmpty;
    print(empty);
  });

@hoc081098 hoc081098 added the waiting for response Waiting for follow up label Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for response Waiting for follow up
Projects
None yet
Development

No branches or pull requests

3 participants