chapter nine
9 Async Dart and Flutter and Infinite Scrolling
In this chapter:
- Futures in Dart
- Streams and Sinks Dart
- AsyncBuilder 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. If you’re okay with the the following code snippits, then you should skip to section 9.2
Listing 9.1. Example Dart code using Streams and Sinks
// Streams and Sinks
StreamController<bool> _controller = new 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 that, then strap in. Async programming is mandatory in modern UI, but it can be difficult to grok at first.
Dart programming makes heavy use of some specific async patterns, known in many languages as "observables". (Observables are also the base 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 say they’re necessary to effective Flutter programming.