Search⌘ K
AI Features

Generator Functions

Explore how Dart generator functions work to generate sequences of values on demand, both synchronously and asynchronously. Understand the use of sync*, async*, yield, and yield* keywords, and learn how to implement recursive generator functions for efficient iteration in Dart.

Introduction

Dart Generator Functions are used to generate a sequence of values on-demand in a lazy manner. Such a value sequence can be generated either synchronously or asynchronously. There are two types of built-in generator functions available to support both scenarios:

  • Synchronous Generator: The synchronous generator function returns an Iterable object. That means the values are generated first and then returned lazily on-demand by the function.

Iterable: A collection of values or “elements”, that can be accessed sequentially. ...