> For the complete documentation index, see [llms.txt](https://wong-coupon.gitbook.io/flutter/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wong-coupon.gitbook.io/flutter/state-management/context-anywhere.md).

# Context Anywhere

During Flutter app development, there are times when you need to access `BuildContext` or navigate from anywhere in your project, even when you’re not inside a widget that has a `context`. This article will guide you on how to set up and use `GlobalKey<NavigatorState>` to access `BuildContext` and perform navigation from anywhere in your application.

## Why Do You Need Global Access to BuildContext?

`BuildContext` is an essential part of Flutter, allowing you to access the widget tree and perform tasks such as:

* Navigating between screens.
* Displaying SnackBars and Dialogs.
* Retrieving theme and media query information.
* If you use Bloc or state management that requires `context`, you can trigger events across different Blocs in your project.

However, in some cases, you need to access `context` from places that are not widgets, such as:

* In service classes, models, or utility classes.
* When handling logic that is not directly related to the UI.
* If using Bloc, you may need to trigger events across different Blocs outside the UI layer.

## **Solution Using GlobalKey\<NavigatorState>**

`GlobalKey<NavigatorState>` allows you to access the global `NavigatorState`, enabling navigation and `BuildContext` retrieval through `navigatorKey.currentContext`.

### **Setting Up navigatorKey**

**Step 1:** Create a global `GlobalKey<NavigatorState>` variable:

```dart
final GlobalKey<NavigatorState> myNavigatorKey = GlobalKey<NavigatorState>();
```

**Step 2:** Provide `navigatorKey` to your `MaterialApp` or `CupertinoApp`:

```dart
return MaterialApp(
  navigatorKey: myNavigatorKey,
  home: MyHomePage(),
);
```

Or, if you are using the `go_router` package:

```dart
final GoRouter ourRouter = GoRouter(
  navigatorKey: myNavigatorKey,
  // ... other configurations
);
```

### **Using navigatorKey to Access BuildContext**

Now, you can access `BuildContext` from anywhere in your code using:

```dart
BuildContext? context = myNavigatorKey.currentContext;
```

**Example:** Navigate to another screen:

```dart
myNavigatorKey.currentState?.pushNamed('/nextScreen');
```

Or use it with `go_router`:

```dart
myNavigatorKey.currentContext?.go('/nextScreen');
myNavigatorKey.currentContext?.read<CameraViewModelBloc>().add(ResetState());
```

## **Advantages and Disadvantages**

### **Advantages**

* **Convenience:** Access `context` and navigate from anywhere.
* **Better Code Organization:** Reduces dependency on widgets for non-UI-related tasks.

### **Disadvantages**

* **More Complex State Management:** Can make state management harder to handle.
* **Scalability Issues:** Overusing global variables in large applications can make maintenance challenging.

[Buy Me a Coffee](https://buymeacoffee.com/ducmng12g) | [Support Me on Ko-fi](https://ko-fi.com/I2I81AEJG8)
