If you need a Flutter file logger, I just came up with the following approach, which seems to work well. First, a little background and a few caveats.
I’ve been working with Flutter and Dart for several weeks now, and I was surprised to read several times that Dart is single-threaded, knowing that it has a concept of a Future (or futures) and async methods. Last night I read this excellent article about Dart’s event loop, which sums up Dart futures very nicely in that statement:
“the code of these Futures will be run as soon as the Event Loop has some time. This will give the user the feeling that things are being processed in parallel (while we now know it is not the case).”
Earlier in the article the author also states:
“An async method is NOT executed in parallel but following the regular sequence of events, handled by the Event Loop, too.”
So, in summary, Dart has a single-threaded event loop, and futures and async methods aren’t handled by a separate thread; they’re handled by the single-threaded event loop whenever it has nothing else to do.
I just wanted to note this here for myself today, but for many more details, please see that article, which also discusses Dart isolates, which are like a more primitive form of Akka actors.
If you ever need to run multiple Dart futures in parallel (simultaneously), I can confirm that this approach works:
As a quick note, here are a couple of examples of how to simulate a slow-responding Flutter method:
I’ll write more about this shortly, but yesterday I created a little video of a demo application I call Futureboard. It’s a Scala/Swing application, but it works like Flipboard in that it updates a number of panels — in this case Java JInternalFrames — simultaneously every time you ask it to update.
The “update” process works by creating Scala futures, one for each internal frame. When you select File>Update, a Future is created for each news source, and then simultaneous calls are made to each news source, and their frames are updated when the data returns. (Remember that Futures are good for one-shot, “handle this relatively slow and potentially long-running computation, and call me back with a result when you’re done” uses.)
Here’s the two-minute demo video:
I’m working on a little app for my Scala & functional programming book I currently call “Future Board.” It works a little like Flipboard in getting news headlines from different sources, but it uses Scala Futures and a few other functional programming techniques.
Here’s a little example of how exceptions work with Scala Futures, specifically looking at the
onComplete ‘Failure’ case.
In this example I start three Futures that run for different lengths of time, and the shortest-running
Future throws an exception:
There are a number of ways to work with Scala Futures, and I provide examples of those approaches in the Scala Cookbook.
If you’re new to Futures, a fun exercise is to try to determine what happens in your code when you use a certain technique. For instance, when you look at the following Scala source code, what do you think it will print?