How to Use BuildContext with Async Safely

If you’ve ever received the DON’T use BuildContext across asynchronous gaps. message in Flutter you’re in good company. I keep getting it, fixing it, then forgetting what I did when I get it next time. The solution is very simple, don’t do that in stateless widgets and always check if the widget is mounted. It is made slightly more complicated by the fact that the docs on the linter error are either wrong or out of date. Build Context The Wrong Way Here’s why this is bad. This is a button handler called from the onPress method of some button in [Read on…]

Simple Time Control For Dart Unit Tests

Controlling time in a unit test allows you to test code that depends on the passage of time like Timer or time comparisons. Dart makes it easy as long as you use the Clock package in code that gets dates instead of using DateTime directly. For any code using Clock time can be controlled through the FakeAsync package. Example Time Test Two key things to note in this example test. First that the test is wrapped in the fakeAsync method. Second that the first time clock is used is inside the fakeAsync wrapper. That’s important, if you access the clock [Read on…]

Flutter Null Safety Unit Tests: Late for Tests

Dart added the late keyword as part of their null safety release and it is very useful, particularly for testing. As a quick review null safety means telling the compiler that some variables will never be null. Any variable declared without a question mark following the type declaration is null safe. So String name is null safe while String? name can be null. Given that the question mark is new syntax all code written before null safety was introduced now declares all variables as cannot be null. The new late keyword tells the compiler that although a variable is not [Read on…]

Null Safety firstWhere

The change to null safety in Dart is a good thing, however a common pattern I used firstWhere for got broken. The pattern is to search a collection and return null if the search returns nothing. The short answer is that you should use firstWhereOrNull instead in this case. Example of the Issue Before the null safe version of Dart the code would look like this: The naive conversion to null safety would be: But this conversion will show a warning that the test if ( null == val ) is not required because the value can’t be null. What’s [Read on…]

Widget Testing and Flutter 1.20.1

After the upgrade to Flutter 1.20.1 none of my widget tests worked anymore. On investigation I found that my widgets weren’t getting rendered at all, i.e. their build method wasn’t being called. This is a preliminary result but I was doing this in my test: Seems like that no longer works and must now be: I haven’t seen anything in the release notes that suggests this should be required. I’m still investigating but in case this helps someone else I thought I’d put it out there.

Compressing POST Requests in Flutter

Posting to REST endpoints from mobile applications is a very common use case. Some of those POST requests can get a little large and it can be useful to compress them before sending to save the user some data. It turns out to be very easy to do this compression in Flutter and I didn’t find resources for it when I went looking so I thought I’d record the steps I took. Code Example This is pretty much just the standard example of using POST. The two things that need to change to send compressed content is to add the [Read on…]

‘What’s My Name’ for Serverless Framework

It is frequently useful to know the name of a generated resource in the Serverless Framework when building for AWS. I had been struggling with this when I needed to refer to a generated resource in a separate CloudFormation section. After much searching I found a very useful trick that is probably already well documented but I’ll add my small bit of amplification here. Finding the Generated CloudFormation Serverless works by generating a CloudFormation template based on the serverless.yml file. This generated file is accessible in a hidden subdirectory of the directory that contains the serverless.yml file if you run [Read on…]

Handling permission requests in Android for Flutter plugins

Updated for the new plugin registration process in Flutter 1.12. While building the Android functionality for a plugin recently I had to handle a system permission request. Although all the parts of that are simple it took me a while to figure out how to put them together. In hopes of making it simpler for others looking to do this here’s all the bits in one place. Note: Code examples are Kotlin, there’s a full gist here. The gist is for pre 1.12, I’ll try to get a post 1.12 gist up in the next little while but until then [Read on…]

Google Sign In and Refresh

One of the dark corners of the OAuth experience is refresh tokens. Every time I play with OAuth based authentication, refresh bites me. Assume some level of hand waving and simplification in what follows as I’m not trying to be perfectly detailed about OAuth. OAuth and Refresh Tokens The way OAuth2 generally works is that after a successful authentication two tokens are generated, an access token and a refresh token. The access token is used to access a service. For example, to make a REST call the developer would include the access token in the header. Access tokens have an [Read on…]

Flutter Logging

The Flutter Logging package is pretty good but it is missing a bit of documentation that I would have found useful. In particular it supports hierarchical logging but I had to look at the code to see how to use it. I have documented what I learned in this post. The Basics Getting logging working is pretty trivial, it works like most logging packages, though it has a few more log levels. For the most up-to-date usage guide see the package documentation. Here’s what I found based on the example at the time I wrote this. Configuring Logging Logging ignores [Read on…]