为什么 flutter return 中的 LicenseRegistry 是 Stream<LicenseEntry> 而不是 List<LicenseEntry>?

Why does the LicenseRegistry in flutter return a Stream<LicenseEntry> and not a List<LicenseEntry>?

我想知道为什么 LicenseRegistry.licenses returns Stream<LicenseEntry> 而不是 List<LicenseEntry>?在这里用一个简单的列表代替 Stream 不是更有意义吗?

dart 文档说明 Stream 如下:

A Stream provides a way to receive a sequence of events. Each event is either a data event, also called an element of the stream, or an error event, which is a notification that something has failed. When a stream has emitted all its event, a single "done" event will notify the listener that the end has been reached.

谁能解释一下许可证列表和流描述之间的关系?

flutter 文档指出 LicenseRegistry.licenses 是一项昂贵的操作。 它 returns 与那个相关的 Stream 是事实吗?

来源:

https://api.dart.dev/stable/2.8.2/dart-async/Stream-class.html https://api.flutter.dev/flutter/foundation/LicenseRegistry/licenses.html

当部分结果对消费者有用时,流很有用(如此处)。在这种情况下,普通消费者是 LicensePage 小部件。它使用 await for 侦听流,并在获取每个包的许可证时使用 setState 将它们添加到页面。通过这种方式,随着工作继续交付更多许可证,它可以开始呈现最终页面。 (如果小部件仍在进行中时被卸载,await for 循环将终止,因此如果关闭许可页面,则不会格式化和呈现页面的其余部分。)

如果 return 值是一个列表,则必须在呈现第一个许可证之前构建该列表。

在一个有趣的转折中,LicensePage 小部件必须从每个 LicenseEntry 中提取段落,这些段落由可迭代项提供。它将它们转换为后台任务中的列表!因此,它处理来自组中特定条目的每组段落。

  final List<LicenseParagraph> paragraphs =
    await SchedulerBinding.instance.scheduleTask<List<LicenseParagraph>>(
      license.paragraphs.toList,
      Priority.animation,
      debugLabel: 'License',
    );