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…]