A few words on the Saturday New York Times crossword

I am back in the grip of the New York Times crossword. I can’t see if this will end happily. Seeing is my challenge, actually. Take 31 down in today’s puzzle, which I started last night, Kitchen…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Using platform variants when testing flutter widgets

As Flutter evolves from being primarily focused on mobile device development to include desktop and the web, it’s increasingly important to test your widgets on all possible platforms.

Flutter makes it easy to check which platform you code is running on using theme.of(context).platform and then checking that against the TargetPlatform enum. When you write your tests, you should make sure your widgets perform or display as expected on any platform.

Historically, the way to do this would be to write your tests with the debugDefaultTargetPlatformOverride setting in your tests and making sure your widgets work as expected. If we use the default flutter app and the default test that is created with it, we can modify the test as shown to ensure that our widget works on iOS (the default is that all test run on Android). Notice also the need to reset debugDefaultTargetPlatformOverride to null otherwise the tests will fail.

This worked well enough when we were making sure that our widgets worked on Android and iOS. But, it starts getting onerous when you want to check if everything runs on all Desktop variants (Linux, Windows, Mac), in addition to all mobile variants. As we move forward, I think it’s best to make sure all your widgets are tested for all platforms from the ground up.

As I said, I just shortened the testWidgets function to save space. Focus on the added variant: parameters. It’s that easy. If I run this test (in VSCode), this is the output in the debug window:

You can see that one run of the test automatically ran the test for all desktop variants that flutter supports. I tend to just use TargetPlatformVariant.all(). It’s so easy to do, and it just makes sure that no unexpected issues popup for a specific platform.

Even if you choose to run tests for all platforms, you will likely encounter situations where you expect different results on different platforms. This is where the previously mentioned debugDefaultTargetPlatformOverride comes in. You can use that to check for a platform, and expect different results. Here’s an example (it doesn’t make much sense in the default app context, but will show you what I mean):

Not the best example, but you should get the point. Even though the variant will run this test on all platforms, there will be different expectations for the iOS version.

I’ve learned and accepted that doing a platform check requires a context and it’s just a fact of life if you want to add tests to your widgets.

As flutter evolves beyond it’s mobile roots into web and desktop, I encourage everyone to test all their widgets on all platforms a matter of course. Also, if anyone has a better/easier way of doing this, please let me know.

Add a comment

Related posts:

The Boomerang Effect of Self Love

A little over ten years ago a book came out entitled “Calling in The One”, in which the central premise was to follow the rules of the law of attraction to bring forth the love that you seek. The…