最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - Create a stream that will emit a single constant value to every subscription once - Stack Overflow

programmeradmin5浏览0评论

I want to create a stream that can emit a single value to every subscriber, no matter when they subscribed.

Ideally, the following code should print out two 42 before done, but none of the given approaches work.

void main() async {
  final stream = createStream(42);
  stream.listen(printValue);
  await Future.delayed(Duration(seconds: 1));
  stream.listen(printValue);

  // Wait for everything to finish.
  await Future.delayed(Duration(seconds: 1));
  print('done');
}

Stream<int> createStream(int val) {
  // Error: Stream has already been listened to.
  // return Stream.value(val);

  // Only gets one output
  return Stream.value(val).asBroadcastStream();
}

void printValue(int val) {
  print(val);
}

Desired output:

42
42
done

But Stream.value() generates an error Stream has already been listened to.. While .asBroadcastStream() gives the wrong result.


Note: Answers using RxDart would be welcomed.

I want to create a stream that can emit a single value to every subscriber, no matter when they subscribed.

Ideally, the following code should print out two 42 before done, but none of the given approaches work.

void main() async {
  final stream = createStream(42);
  stream.listen(printValue);
  await Future.delayed(Duration(seconds: 1));
  stream.listen(printValue);

  // Wait for everything to finish.
  await Future.delayed(Duration(seconds: 1));
  print('done');
}

Stream<int> createStream(int val) {
  // Error: Stream has already been listened to.
  // return Stream.value(val);

  // Only gets one output
  return Stream.value(val).asBroadcastStream();
}

void printValue(int val) {
  print(val);
}

Desired output:

42
42
done

But Stream.value() generates an error Stream has already been listened to.. While .asBroadcastStream() gives the wrong result.


Note: Answers using RxDart would be welcomed.

Share Improve this question edited Feb 7 at 16:15 DarkBee 15.6k8 gold badges70 silver badges115 bronze badges asked Feb 7 at 16:11 ph3rinph3rin 4,8501 gold badge22 silver badges45 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can achieve the desired output by using a multi-subscription stream:

void main() async {
  final stream = createStream(42);
  stream.listen(printValue);
  await Future.delayed(Duration(seconds: 1));
  stream.listen(printValue);

  // Wait for everything to finish.
  await Future.delayed(Duration(seconds: 1));
  print('done');
}

/// Returns a multi-subscription stream.
Stream<int> createStream(int val) => Stream.multi((streamController) {
      streamController.add(val);
    });

void printValue(int val) {
  print(val);
}

The console output is listed below:

$ dart main.dart 
42
42
done

Note: Another easy way to create a multi-subscription stream is by using the factory constructor Stream.fromIterable:

/// Returns a multi-subscription stream that yields only `val`.
Stream<int> createStream(int val) => Stream.fromIterable([val]);
发布评论

评论列表(0)

  1. 暂无评论