← Back to posts Cover image for Fixing the 'Don't Use BuildContexts Across Async Gaps' Warning in Flutter

Fixing the 'Don't Use BuildContexts Across Async Gaps' Warning in Flutter

· 5 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

You’ve probably seen it: a yellow squiggle in your IDE or a warning in your console telling you, “Don’t use ‘BuildContext’s across async gaps.” It’s one of those Flutter lint warnings that can feel a bit cryptic at first. Is it a hard error? No. Should you care? Absolutely. Ignoring it can lead to one of the most common and frustrating bugs in Flutter: trying to use a BuildContext that’s no longer valid, resulting in a StateError or silent failures when calling things like Navigator.of(context) or ScaffoldMessenger.of(context).

Let’s break down why this happens and, more importantly, how to fix it the right way.

Why Does This Warning Exist?

In Flutter, a BuildContext is inherently tied to the lifecycle of a widget. When a widget is removed from the tree—say, because a user navigated to a new screen, a Future completed and triggered a rebuild, or a conditional in your UI removed a widget—its BuildContext becomes disposed. It’s no longer valid.

An “async gap” is any point where your synchronous Dart code pauses, like during an await statement. If you capture a BuildContext before the gap and try to use it after the await, you are gambling that the widget is still in the tree. If it’s not, your app crashes or behaves unexpectedly.

Here’s the classic offender:

void _loadUserData(BuildContext context) async {
  final data = await fetchUserData(); // Async gap!
  // WARNING: Using 'context' here is dangerous.
  Navigator.of(context).push(MaterialPageRoute(builder: (_) => DetailsPage(data)));
}

In this snippet, if the user leaves the current screen while fetchUserData() is running, the original context will be disposed by the time Navigator.of(context) is called, throwing an error.

Solution 1: Check mounted (for StatefulWidget)

If you’re inside a StatefulWidget, you have access to the mounted property. This boolean tells you if the widget’s State is currently in the tree. It’s a simple and effective guard.

Future<void> _deleteItem() async {
  try {
    await repository.deleteItem(itemId); // Async gap
  } catch (e) {
    // Check mounted before using context
    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Deletion failed: $e')),
    );
  }
}

Always check mounted after the await and before any operation that uses context. This is a defensive pattern you should adopt in all your State methods that perform async operations.

Solution 2: Move Context Usage Before the Async Gap

Often, you can restructure your logic to use the BuildContext immediately, before any asynchronous operation begins. This is safest because there’s no gap.

void _showConfirmationDialog() {
  // Use context synchronously to show the dialog
  showDialog(
    context: context,
    builder: (ctx) => AlertDialog(
      title: Text('Confirm'),
      actions: [
        TextButton(
          onPressed: () async {
            Navigator.pop(ctx); // Close dialog using its own context
            await _performDeletion(); // Async gap, but we're not using the outer context
            // If you need to show another SnackBar, you'd need a new approach.
          },
          child: Text('Delete'),
        ),
      ],
    ),
  );
}

Here, the context from the original widget is used only synchronously to show the dialog. The async work happens later, safely isolated from the original widget’s lifecycle.

Solution 3: Use a State Management Solution

For more complex flows, consider moving async logic outside the widget tree entirely. Using a state management package like riverpod or bloc can help.

Here’s a simplified riverpod example:

final deleteItemProvider = FutureProvider.autoDispose<void>((ref, String itemId) async {
  try {
    await ref.read(repositoryProvider).deleteItem(itemId);
  } catch (e) {
    // Logic here is not dependent on any widget's BuildContext
    // You can handle errors in the UI via ref.watch/listen
    throw e;
  }
});

// In your widget
Consumer(builder: (context, ref, child) {
  ref.listen(deleteItemProvider, (previous, next) {
    // This listener is safe; it reacts to state changes.
    if (next.hasError) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: ${next.error}')),
      );
    }
  });
  return YourUI();
});

By decoupling business logic from the UI, you avoid holding references to BuildContext across async gaps. The UI simply reacts to state changes.

Solution 4: Use Callbacks or Events

Sometimes you can pass a callback function that executes after the async work completes, using a BuildContext that is guaranteed to be valid at the time of the callback.

class _MyWidgetState extends State<MyWidget> {
  Future<void> _taskWithCallback(void Function() onSuccess) async {
    await Future.delayed(Duration(seconds: 2));
    onSuccess(); // Executed synchronously after the gap
  }

  void _startTask() {
    _taskWithCallback(() {
      if (mounted) {
        // Safe because we check mounted and are in a synchronous callback
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Done!')));
      }
    });
  }
}

Key Takeaway

The warning is a helpful guardrail. The core principle is: Never assume a BuildContext remains valid across an await. Adopt one of these patterns:

  1. Check mounted (in StatefulWidget).
  2. Use context synchronously before the async gap.
  3. Decouple logic with state management.
  4. Use safe callbacks.

By making these patterns habit, you’ll eliminate a whole class of lifecycle bugs and make your Flutter apps more robust. The next time you see that yellow squiggle, you’ll know exactly how to tackle it.

This blog is produced with the assistance of AI by a human editor. Learn more

Related Posts

Cover image for Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Developers frequently struggle with persistent storage in Flutter, especially when dealing with OS-level backups and app reinstalls. This post will clarify the nuances of different storage solutions like `SharedPreferences`, `path_provider`, and `flutter_secure_storage`, focusing on how to correctly manage data lifetimes (per-install vs. per-user) and avoid issues like device IDs migrating to new phones or data being unexpectedly backed up.

Cover image for Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Flutter's `ImageFiltered` can cause significant lag when blurring large or frequently changing content like text. This post will explain the performance bottlenecks, provide solutions like caching blurred outputs with `RepaintBoundary` or `SnapshotWidget`, and offer practical tips to ensure smooth UI effects without sacrificing frame rates.