iterable sequences in Flutter Devices

What does ‘yield’ keyword do in flutter?

In Dart, the yield keyword is a powerful feature used within generator functions to produce multiple values lazily, rather than returning them all at once. This makes it highly efficient for working with iterable sequences and streams, ensuring better performance and memory optimization.

Generator functions using yield play a crucial role in asynchronous programming, particularly in Flutter applications. They are commonly utilized when handling real-time data streams, event-driven architectures, and state management solutions such as the Bloc (Business Logic Component) pattern. By leveraging yield, developers can create responsive, efficient, and scalable applications that seamlessly process continuous data flows.

How Yield Works in Dart

In Dart, the yield keyword is used inside functions that return an Iterable or Stream. When a function contains yield, it becomes a generator function, meaning that it does not execute all at once. Instead, it pauses execution and produces values lazily, emitting one value at a time when requested.

This lazy evaluation approach helps improve performance and memory efficiency, especially when dealing with large datasets or real-time event streams.

Using yield in a Synchronous Generator

A synchronous generator function uses sync* and returns an Iterable. Each time yield is called, the function pauses and waits until the next value is requested.

Iterable<int> generateNumbers(int count) sync* {
  for (int i = 0; i < count; i++) {
    yield i; // Returns one number at a time
  }
}

void main() {
  for (var num in generateNumbers(5)) {
    print(num);
  }
}
Output:

0

1

2

3

4

Using yield in an Asynchronous Generator

An asynchronous generator function uses async* and returns a Stream. It is useful for working with asynchronous data flows, such as listening to real-time updates.

Stream<int> generateNumbersAsync(int count) async* {
  for (int i = 0; i < count; i++) {
    await Future.delayed(Duration(seconds: 1)); // Simulate delay
    yield i; // Emits one number at a time asynchronously
  }
}

void main() async {
  await for (var num in generateNumbersAsync(5)) {
    print(num);
  }
}
Output

0

1

2

3

4

yield*: Delegating Iterators or Streams

Dart provides the yield* keyword, which allows a generator function to delegate its execution to another iterable or stream.


Iterable<int> numbersFromOneToThree() sync* {
  yield* [1, 2, 3]; // Delegates values from the list
}

void main() {
  for (var num in numbersFromOneToThree()) {
    print(num);
  }
}
Output:

1

2

3

Practical Use in Flutter

In Flutter, yield is commonly used with the Bloc (Business Logic Component) pattern to manage state changes efficiently. Below is an example using Bloc and Stream:

import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';

// Defining States
abstract class CounterState {}
class CounterInitial extends CounterState {}
class CounterUpdated extends CounterState {
  final int counter;
  CounterUpdated(this.counter);
}

// Defining Events
abstract class CounterEvent {}
class Increment extends CounterEvent {}

// Creating Bloc
class CounterBloc extends Bloc<CounterEvent, CounterState> {
  CounterBloc() : super(CounterInitial());

  @override
  Stream<CounterState> mapEventToState(CounterEvent event) async* {
    int counter = 0;
    if (event is Increment) {
      counter++;
      yield CounterUpdated(counter); // Emits new state lazily
    }
  }
}

Conclusion

  • The yield keyword in Dart is used to return values lazily from generator functions.
  • It is useful in both synchronous (sync*) and asynchronous (async*) generators.
  • The yield* keyword helps delegate iterable or stream values from another generator.
  • yield is commonly used in Flutter with Stream and Bloc for efficient state management.

Recent Blogs


Posted

in

by

Tags:

To Know Us Better

Browse through our work.

Explore The Technology Used

Learn about the cutting-edge technology and techniques we use to create innovative software solutions.