flutter

Posts related to Flutter and Dart

An attempt at a Flutter file logger using Dart isolates alvin November 19, 2019 - 4:12pm

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:

A Flutter file logger (for iOS and Android simulators) alvin November 13, 2019 - 8:32pm

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.

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 alvin October 27, 2019 - 6:41pm

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.

Dart: How to return a known, constant, or literal value in a Future

Sometimes a Dart or Flutter API may require you to return a Future even if you already have a known, constant, or literal value. If/when that happens, you can use this approach:

return new Future(() { return 42; });

In this example the known value is 42, but it could have been a string like "Hello", a boolean like true, or any other known or literal value.

A Dart string capitalize function

I was surprised to learn that Dart doesn’t have a string capitalize method, so if you ever need a capitalize method/function, here’s a start towards a solution:

String capitalize(String s) => s[0].toUpperCase() + s.substring(1);

That function will work with Dart strings like these:

A Dart Isolates example (Actors in Dart)

Dart Isolates give you a way to perform real multi-threading in Dart applications, the ability to truly run code on another thread. (For more information on that statement, see Dart futures are NOT run in a separate thread.)

When I first read that Isolates were like actors I was very interested in them, but then I was surprised to see that (IMHO) they are implemented in a more low-level/primitive way than something like the Akka Actors library. (The entrepreneur out there might see this as an opportunity to create an Akka-like port for Dart and Flutter.)

A Dart function to get the current date/time in a “seconds since the epoch” format alvin October 10, 2019 - 3:39pm

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();
}

Dart: How to remove leading spaces in a multiline string (using splitMapJoin)

As far as I know, there’s no built-in way to remove spaces from a multiline string in Dart, but you can write your own function to do this. For example, I just dug into the splitMapJoin method of the Dart String class, and wrote a stripMargin function like this:

String stripMargin(String s) {
    return s.splitMapJoin(
        RegExp(r'^', multiLine: true),
        onMatch: (_) => '\n',
        onNonMatch: (n) => n.trim(),
    );
}