Monday, January 10, 2022

Flutter in Action Chapter 9 "Async Dart and infinite Scrolling"

Completed taking notes on "Flutter in Action" chapter 9 "Async Dart and Infinite Scrolling".

https://www.manning.com/books/flutter-in-action

To quote the author, "Features all rely on each other to explain in a circular manner."  That is the fundamental problem with async programming, you either get all of it and its all crystal clear, or get none of it and its all unclear.

The chapter starts with an introduction to Future as the base of async programming in Dart.  Give it a .then and a function to call when the data is eventually ready.  Give it a .catchError to process failure arriving rather than good data arriving.

Next up is a discussion of async to turn a function async, and await to make little parts inside it synchronous again.  Supposed to be easier or more clear to read than using Futures.

Error handling in async programming can be done with try/catch blocks or placing a .catchError on a Future.

The chapter repeats a couple times what's required for a working Observer/Stream pattern, which is good because its important.  The first is a sink for events, Sink being the abstract class and mostly using StreamController as the sink.  The second requirement is streams which are properties on the sink.  Final requirement is subscribers / observers / listeners which wait for notification by listening to the stream.  This section has great examples which build up from simple examples to rather complicated multi-subscriber systems using StreamController.broadcast() using an example of a hamburger restaurant.  I especially like how the cartoon illustrations gradually have more complex source code added to them to describe the async operation of the restaurant's kitchen.  Very clear and detailed explanation of the Observer Pattern.

Higher order streams are analogous to higher order functions; streams which return streams.  A bit complicated and even a simple example is a page of dense code.  It would have been nice to arrange the chapter to cover a minimal set of circular dependencies and later cover this complicated feature.  Then again the usual problem that its already a 342 page long book so this topic had to be fit somewhere.

StreamBuilder turns a stream of data into widgets, similar to a template library.  It is a close cousin of the FutureBuilder.  The book glosses over how to use and set it up, probably a page length limitation.  If the book had the luxury of ten additional pages to cover this one page topic, it would probably look like the tutorials I'll link below:

https://www.woolha.com/tutorials/flutter-using-streambuilder-widget-examples

https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html

I think the topic lends itself well to video presentation as seen in the video on the API page above.  I like that video.

Infinitely scrollable lists are made with CustomScrollView.  It wants a source of Slivers and some type of scroll physics.  Sadly the chapter lists scroll physics as a topic to be covered on the very first page of the chapter but its not discussed; the book already has 342 pages... probably got edited out to save space.  Slivers are widgets with a weird lifecycle, they are created and deleted based on if they're in the user's view of the list, which makes them fast and efficient.  Regular widgets would be slower and use more memory.  Begs the question of why Flutter does not make caching decisions automatically for the programmer; why not let the framework decide instead of hard coding it into the source code?

The book unfortunately misses a great chance to compare/contrast a ListView vs the CustomScrollView.  Main differences are ListViews don't sliver and their widgets like Row/Container for arrangement, whereas CustomScrollView does sliver and has its grid passed separate from its slivers.  Supposedly you can do things like appbar animations while scrolling with CustomScrollView although I've never done this myself.  That would likely be too advanced of a topic for the current length of the book.

I think the best way to read this chapter is to close that circularity problem; read it all, get the big picture locked down, THEN research in detail, like my example above of doing a compare and contrast online of ListView vs CustomScrollView or digging in deep for an hour or two of online docs and videos about how to use StreamBuilder.  Async programming is a big topic; someone could probably make an entire book out of going into this chapter in great detail; I'd buy that book.  On a side note I have a pretty good book about async programming on Python so I know its a book length topic...  When "Python Concurrency with asyncio" is officially published in a couple months I might review it.  I've been reading that MEAP book as chapters have been released and its a good book.  Anyway Dart / Flutter could use a similar book which would expand this chapter to book length.  I see the Python async book is already up to 376 pages in pre-publishing which is longer than this entire Flutter book, LOL.

Next up, chapter 10, "Working with data: HTTP, Firestore, and JSON".