9 Async Dart and Flutter (and some scrolling tips)
In this chapter:
- Futures in Dart
- Streams and Sinks Dart
- AsyncBuilder in Flutter
- Slivers and Scrolling
- 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
// Futures var val = new Future<bool>(); // Streams and Sinks StreamController<bool> _controller = new StreamController<bool>(); Stream get onEvent => _controller.stream; void handleEvent() { onEvent.listen((bool 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, so I’d expect that you’ve used it at some point before now. But, Dart programming makes heavy use of some specific async patterns, known in most languages as "observables".
You won’t get very far into any Dart code before you have to start dealing with streams, which is what Dart calls observables. And in fact, I’d say they’re necessary to effective Flutter programming.
Futures, completers, async for, streams, sinks and the listen
keyword are all part of the same "part" of the Dart language. These features are all similar or related. Some (if not all) of these classes and features are built ontop of eachother, with Future
being the basic building bloc.