Posts in the “flutter” category

Dart/Flutter: A simple delta-time performance debugging technique

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.

Using a SQLite date/time field with Flutter and Dart

As a brief note, at the time of this writing, 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.

Solution: 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.

How to create a Dart project manually

If you want to create a Dart project, probably the best way to do that currently is to use Stagehand:

Once you install that you can create new Dart command-line applications like this:

// create a command-line application
stagehand console-full

However, if you just want to create a little Dart project manually, you can also just follow the steps below:

Dart/Flutter: How to create an immutable list?

If you come from another programming language like Scala where you can easily create immutable lists — and many other immutable things — working with Dart (and Flutter) can be a major change, possibly because Dart was designed to be a pure OO language.

That being said, I just saw that there is a way to create an immutable list in Dart:

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

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