Scala, Java, Unix, MacOS tutorials (page 38)

I don’t agree with everything Nathaniel Hackett has done to start his tenure with the Denver Broncos, but I do agree with this quote about trying to create a good and happy work environment:

“You want them to feel an environment that they want to come into, and they’re excited to come into. That’s all you can do, that’s what you’re trying to create.”

I’m creating this page as a collection of Flutter source code snippets. These are early days, but here you go.

Note also that my Flutter Quick Reference has many more examples as well.

Replacement home page

Here’s a link to my Flutter replacement home page code.

This may not make sense to most people, but if you ever want a simple replacement Flutter “home page” for a new app, this should work. All it does is create a home page with a ListView, and you can use those ListView items as simple menu items to get started with.

I also added a FloatingActionButton in case you want that to start with as well. Note that you’ll probably want to remove the @dart line for modern Flutter apps. (I’ll try to remember to update this code the next time I use it.)

I prefer this to the default home page you get when running flutter create my_app. My goal is to be able to highlight the default Flutter home, delete that code, and replace it with this code as a simple starter menu.

Here’s the code:

In one of the crazier bugs I’ve seen, I got to a point where I had my Flutter app in good shape, and I had been testing it on an Android device. So as one last test I wanted to make sure I deleted the app from the Android device — a Kindle Fire tablet — so I could then do a complete reinstall of my app so I could give it one more quick test.

And then it never installed properly.

The app wouldn’t install on the device

It seemed like the app started to install on my Android device, but then it wouldn’t. I tried all sorts of different flutter run commands, but the app just wouldn’t install. It seemed a little like it was hung up (stalled), but it also seemed a little like it also finished installing and was sitting there waiting for me. (At the moment I don’t remember the exact messages the Flutter build process was showing.)

Dart/Flutter tip: As a brief note today, if you ever need to debug some Dart/Flutter code to see how much time that code is taking (such as to measure performance), here’s a manual approach you can use to determine the “delta time” between two Dart statements:

final t1 = DateTime.now();
// the source code you
// want to test
// is in here
final t2 = DateTime.now();
debugPrint('DELTA TIME: ${t2.difference(t1)}');

I just used this code to find a significant performance bottleneck, so while it’s not as sophisticated as other techniques, it was a nice performance-debugging tool today.

One thing I like about using Flutter to build mobile apps is that like Scala, it’s low-ceremony: every character has a purpose that isn’t redundant. It’s also a modern take on creating UIs, where your code is just widgets, event-handling, and custom logic.

For example, with a little extra file/directory logic that’s not shown here, this code is all you need to list the files in a directory on both iOS and Android devices.

How Flutter is like Scala (and why I like them both)

Half of my DNA is Assyrian, and I just learned that the Assyrian New Year is April 1st, so to celebrate that I’ve reduced the price of both the Print and Kindle versions of my book, Functional Programming, Simplified. I’ll keep the prices this low until April 15, 2022, so be sure to grab a copy before then.

SALE: Functional Programming, Simplified

As a brief note, these are two different ways to do a “sleep” call in Flutter/Dart, depending on your needs:

// inside an async method
await Future.delayed(const Duration(milliseconds: 250));

// not in an async method
sleep(const Duration(milliseconds: 250));

As shown, the first approach is for when you want an async/await approach, and the second sleep method is for when you are doing things in serial order (as opposed to a parallel/concurrent approach).

One of the keys when writing Flutter code is to know when to call the setState method. As a quick answer, the Flutter docs state:

“Yet when consciousness and the body are brought into union with one another, the energy of consciousness becomes still, and when the energy of consciousness is still, consciousness too is still, and the soul pervades the entire body.”

~ The Tree of Yoga, B.K.S. Iyengar

As a brief note, I ended up not using the following code, but if you ever need to use the Flutter url_launcher library with the Flutter Html widget’s onLinkTap method, this code should work for you:

As a note to my Future Self, any time you’re working with Flutter and you want to try to make initState or build an async method — or try to use the then method on a Future inside initState or build — what you probably really want is to use a FutureBuilder inside the build method, something like this:

As a quick note, this Flutter/Dart example shows a class constructor that uses named parameters:

As a brief note, these are some string utility functions that I’ve been using in a Flutter/Dart app. I keep them in a file named string_utils.dart:

“You’ve got a new face, new fingerprints, even a new name. Williams. Remo Williams. A lot of thought went into it.”

~ a little humor for those in the know

The origin of the name Remo Williams

As a quick Flutter FutureBuilder note, I wrote a FutureBuilder preferences example, but by the nature of that topic, the code isn’t necessarily simple.

Today I just saw this much simpler FutureBuilder example, so I want to give it props. Besides the clean code, the important concept in that article is that you know that your widget data is coming from an async source — such as a REST call or database access — an you need to know how to handle that.

As a brief note, sometimes in Flutter (and Dart) you have to write a method/function that makes an asynchronous call, but the method can’t be marked async itself. The Flutter initState method is a good example of this.

So far I’ve learned two ways to deal with this:

  • Use then inside the non-async method
  • Use an unusual anonymous-function style syntax inside the method

I’ll demonstrate each technique below.

As a brief note to self, here’s an example of the forEach method on a Dart Map class. First, a sample Dart Map:

final states = {
    'AK' : 'Alaska',
    'AL' : 'Alabama', 
    'AR' : 'Arizona'
};

As a brief note to self, the Dart/Flutter filter method on a List is actually named where. I’m used to this type of method being named filter in other programming languages, but where is cool, too. Here’s a one-line where example:

final List<File> files = entities.where((e) => (e is File)).toList();

Note: Dart compared to Scala

FWIW, in Scala that code would use a filter method instead of where, and look like something like this:

val files: List<File> = entities.filter(e => e.isFile));

Or more simply, like this:

val files = entities.filter(_.isFile));

But getting back to my point, Dart/List/filter is really Dart/List/where.