Posts in the “flutter” category

Using a SQLite date/time field with Flutter and Dart

As a brief note, SQLite doesn’t have date/time (datetime) fields, so when you want to use a datetime field with Flutter and Dart, you have to do something else.

My choice was to store my date/time data as a SQLite INTEGER field. I did this so I can query and sort that field easily myself. The benefit of storing a datetime field as a TEXT field is that you can read it easier, but I think an INTEGER field is a little easier to work with, though YMMV.

Flutter/Dart: A simple FutureBuilder async widget example

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.

An attempt at a Flutter file logger using Dart isolates

Filed in the “FWIW” category ... I wrote the following Dart/Flutter code as an attempt to create a Flutter file logger using Dart isolates, but as it turns out, at the time of this writing the Flutter platform won’t let you do that. So I thought I’d share/save my code here in case I can use it in the future.

First, here’s my Flutter file logging code that uses Dart isolates:

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: An initial home page using ListView, ListTile, and FloatingActionButton

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:

Flutter: Can’t install app on Android device (Kindle Fire tablet)

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: A simple delta-time performance debugging technique

As a brief note today, if you ever need to debug some Dart/Flutter code to see how much time some code is taking, here’s a manual approach that you can use to determine the “delta time” between two 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 bottleneck, so while it’s not as sophisticated as other techniques, it was a nice performance-debugging tool today.

The Dart List 'filter' method is named 'where'

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.

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

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.

Flutter/Dart: How to sleep for X seconds/milliseconds

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).

Dart example: Factory pattern, constructors, mixins

I’m generally getting out of the writing business, but that being said, I don’t mind sharing some code as I go along. In this case I created the follow Dart programming examples as I reload Dart into my brain:

  • Dart Factory pattern/method example
  • Dart constructors with public and private fields
  • Dart mixin example

Flutter/Dart: A few ways to simulate slow-responding functions and methods

As a quick note, here are a couple of examples of how to simulate a slow-responding Flutter/Dart method:

Future<bool> _getFutureBool() {
    return Future.delayed(Duration(milliseconds: 500))
                 .then((onValue) => true);
}

Future<bool> getEnableNotifications() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return Future.delayed(const Duration(milliseconds: 1000), () {
        return prefs.getBool(KEY_ENABLE_NOTIFICATIONS) ?? false;      
    });
}

As shown, the first example uses Future.delayed followed by a then call, and the second example shows Future.delayed with a lambda function.

You’ll want to use this code/technique when you’re working with Flutter futures and its FutureBuilder class.

I was reminded about the need to test things likes futures and FutureBuilder when some code I found on the internet wasn’t working properly. As a result I wrote my tutorial, How to correctly create a Flutter FutureBuilder.

Flutter/Dart: How to use async code inside a non-async method (like initState)

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.