concept stream in category flutter

appears as: streams, A stream, Stream, stream
Flutter in Action

This is an excerpt from Manning's book Flutter in Action.

The state object also has a method called initState, which is called as soon as the widget is mounted in the tree. State.initState is the method in which you initialize any data needed before Flutter tries to paint it the screen. For example, you could subscribe to streams or compute some data into a human-friendly format.

Listing 9.1. Example Dart code using streams and sinks
// Streams and Sinks
StreamController<bool> _controller = StreamController<bool>();
Stream get onEvent => _controller.stream;
 
void handleEvent() {
    onEvent.listen((val) => print(val));
}
 
// async / await
void getRequest() async {
    var response = await http.get(url);
    return response;
}

Dart programming makes heavy use of a specific async pattern, which is known in many languages as observables. (Observables are also the foundational concept of Rx libraries.) In Dart, observables are called streams. You won’t get very far into any Dart code before you have to start dealing with streams. And, in fact, I’d claim they’re necessary for effective Flutter programming.

Streams aren’t the only async feature in Dart, though. Futures, completers, streams, sinks, the async and await keywords, and the listen keyword are classes or features of Dart that you’ll see often, and they all provide asynchronous functionality. Streams are probably the hardest to grok, though, so we’ll start with the basics and build up to streams.

But this hypothetical hamburger shop serves roughly 10,000 different food items. Oh, and the cooks are actually robots that only understand binary. This means the human server needs to emit information in a stream, but that stream needs to be fed into a translator, which will manipulate the data and then output a new stream with the same information in binary. This is pictured in figure 9.6.

Figure 9.6. Stream transformers are used to manipulate the values from a stream and output them as a new stream.

You can find this code in the repository at chapter_9/stream_translator/. There are two files: main.dart houses all the code that’s simply been updated for this example, such as the BurgerStand and Cook classes, and translator.dart holds the new class that translates streams.

Listing 9.9. GoodBurgerRestaurant class
class GoodBurgerRestaurant {
  Cook cook = Cook();
  StreamController _controller = StreamController.broadcast();
  Stream get onNewBurgerOrder => _controller.stream;
 
  void turnOnTranslator() {
    onNewBurgerOrder
        .transform(BeepBoopTranslator())              #1
        .listen((data) => cook.prepareOrder(data));   #2
  }
 
  void newOrder(int orderNum) {
    _controller.add(orderNum);                        #3
  }
}
  • StreamController objects are used to define streams and sinks.
  • A Sink is the entry point of data for a stream.
  • A Stream is what other pieces of code listen to in order to get data from streams as it becomes available. Streams can be transformed, and functions that take in streams and output a stream of transformed data are called higher-order streams.
  • A bloc is a business logic component that relies on streams to build inputs and outputs your widgets can interact with as the state management logic in your widget.