flutter

Flutter: Override widgets/components to get access to events

As a brief “note to self,” when you need to get access to Flutter widget events that you normally can’t access, override the widget to access lifecycle-related events like initState() and dispose() so you can access them. For example, this image (that comes from this URL) shows how to gain access to those lifecycle methods for a Flutter Drawer. You can do this with any component/widget — and you can also make the code more generic by passing in child widgets — and you can also mix in WidgetsBindingObserver and then use didChangeAppLifecycleState to gain access to more events.

Flutter Quick Reference

Flutter Quick Reference - book cover

When I started working with Flutter a couple of months ago to develop iOS and Android apps from one code base, I started keeping notes about how to do things with Flutter and Dart (the programming language behind Flutter). These notes became my own personal cheatsheet, and then those notes just kept getting larger, and larger.

From there, I started to create a book I titled “Flutter Quick Reference” based on those notes. Right now this “book” is really just a very large Flutter/Dart cheatsheet, but because some of the content in it can’t be found elsewhere on the internet, I thought I’d share it here. Also, because I don’t know if I’ll ever take the time to finish making this into a real book, I made this first release free.

Flutter/iOS App Store error message: ITMS-90338: Non-public API usage alvin October 19, 2019 - 4:47pm

As a brief note today, if you’re using Flutter and get this “ITMS-90338: Non-public API usage” App Store Connect error message, it probably means you forgot to run flutter build ios before submitting your app to the App Store:

A Dart function to get the current date/time in a “seconds since the epoch” format

As a brief note, if you need a Dart function to get the current date/time in a “seconds since the epoch” format, I can confirm that this function works:

/// the current time, in “seconds since the epoch”
static int currentTimeInSeconds() {
    var ms = (new DateTime.now()).millisecondsSinceEpoch;
    return (ms / 1000).round();
}

Flutter, sqflite, and escaping quotes with SQL INSERT and UPDATE statements

When you want to do a SQL INSERT or UPDATE using the Flutter sqflite package, I’ve found that it’s best to use their insert and update methods (as shown below) so you don’t have to worry about escaping single- and double-quotes. This is similar to the reason why Java developers switched from Statement to PreparedStatement way back in the late 1900s.

As an example, given this SQLite database table:

Dart futures are NOT run in a separate thread (they are run in the event loop)

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.

When you have Flutter app performance problems, try Profile Mode

Today I learned that for several reasons, Flutter Debug mode may be significantly slower than Production mode. As just one reason, Debug mode is compiled using JIT while Production mode uses AOT. You can read more about the reasons on the Flutter UI Performance page.

A very important note on that page is that if you’re using a real hardware device (as opposed to an emulator), you can run your code in Profile mode like this:

Flutter/Dart: How to convert TimeOfDay fields (and convert to DateTime) alvin September 25, 2019 - 11:40am

When using Flutter (and Dart), if you need to compare two TimeOfDay values, I can confirm that it helps to first convert the TimeOfDay to a double using this function:

/// note: 'hour' is in 24-hour format
double _timeOfDayToDouble(TimeOfDay tod) => tod.hour + tod.minute/60.0;

Solution: When AndroidManifest.xml changes aren’t seen in your Flutter dev/test app

As a brief note, if you’re developing an Android app with Flutter and then find that the changes you made to your AndroidManifest.xml file aren’t being seen in your app, you’ll probably need to uninstall your dev/test app and completely reinstall it with flutter run. I just ran into this problem while working with flutter_local_notifications — which requires changes to AndroidManifest.xml to work properly — and uninstalling and reinstalling the app fixed the problem.