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.

9.1  Async Dart

9.1.1  Future Recap

9.1.2  async / await

9.2  Sinks and Streams (and StreamControllers)

9.2.1 Implementing streams

9.2.2  Broadcast streams

9.2.3  Higher Order Streams

9.3  Using streams in blocs

9.3.1  Implementing a bloc input

9.4  Async Flutter: StreamBuilder

9.5  Infinite and Custom Scrollable widgets

9.5.1  CustomScrollView and Slivers

9.5.2  CatalogWidget scroll view

9.5.3  Delegates

9.5.4  Custom sliver

9.6  Summary

sitemap