Back to results for “Flutter Performance Optimization”
Flutter › Dart Streams & StreamControllers

What are common StreamTransformer patterns, such as debounce and throttle, and how do they help control the actual rate at which a stream's events are processed, particularly for something like a rapidly typed search query?

Advanced

Answer

A debounce transformation delays processing a stream's most recently emitted value until a specified pause in new incoming events has genuinely occurred, meaning it effectively waits until a user has actually stopped typing for a brief moment before triggering an actual search, while a throttle transformation instead limits event processing to occurring at most once within a given fixed time window regardless of how many individual events actually occur during that window, and both of these patterns help meaningfully reduce unnecessary, wasteful work, such as avoiding firing off a brand new network request on every single individual keystroke a user types.
searchController.stream
  .debounceTime(Duration(milliseconds: 300))
  .listen((query) => performSearch(query));
Real-world example A product search feature debounces its stream of text input changes by three hundred milliseconds, waiting until the user has genuinely paused typing before actually triggering a network request, significantly reducing the total number of unnecessary search requests sent to the server.

Common follow-ups: What package is commonly used in Flutter to easily add debounce and throttle support to a Stream?;How do you decide on an appropriate debounce duration for a specific search feature?