This chapter covers
- Futures in Dart
- Streams and sinks in Dart
- Async builder in Flutter
- Slivers and scrollable widgets
- Scroll physics
This chapter could contain the most difficult concepts to wrap your head around, unless you’re familiar with async UI programming. The beginning of this chapter is only about asynchronous concepts and implementing them in Dart. After that, I examine how those concepts are useful in Flutter.
Note
If you’re okay with the following code listing, then you should skip to section 9.2, where we begin the Flutter portion.
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; }
If you aren’t comfortable with this listing, then strap in. Async programming is mandatory in modern UIs, but it can be difficult to grok at first.
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.