# FlutterError: 'const' key not found in InheritedWidget context

- **ID:** `flutter/const-key-not-found`
- **Domain:** flutter
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 75%

## Root Cause

A widget used a const key to access an InheritedWidget, but the InheritedWidget was not in the widget tree above the accessing widget, often due to missing provider or incorrect build context.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Flutter 3.10.0 | active | — | — |
| Flutter 3.19.0 | active | — | — |
| Dart 3.0.0 | active | — | — |

## Workarounds

1. **Ensure the InheritedWidget is placed above the accessing widget in the widget tree. For example, wrap the relevant subtree with a provider:
  class MyProvider extends InheritedWidget {
    final String data;
    MyProvider({required this.data, required Widget child}) : super(child: child);
    static MyProvider of(BuildContext context) {
      return context.dependOnInheritedWidgetOfExactType<MyProvider>()!;
    }
    @override
    bool updateShouldNotify(MyProvider oldWidget) => data != oldWidget.data;
  }
Then use MyProvider in the build method before accessing it.** (85% success)
   ```
   Ensure the InheritedWidget is placed above the accessing widget in the widget tree. For example, wrap the relevant subtree with a provider:
  class MyProvider extends InheritedWidget {
    final String data;
    MyProvider({required this.data, required Widget child}) : super(child: child);
    static MyProvider of(BuildContext context) {
      return context.dependOnInheritedWidgetOfExactType<MyProvider>()!;
    }
    @override
    bool updateShouldNotify(MyProvider oldWidget) => data != oldWidget.data;
  }
Then use MyProvider in the build method before accessing it.
   ```
2. **Verify that the BuildContext used to access the InheritedWidget is from a descendant widget. Call 'context.dependOnInheritedWidgetOfExactType' only after the InheritedWidget has been inserted into the tree, e.g., in a child widget's build method.** (80% success)
   ```
   Verify that the BuildContext used to access the InheritedWidget is from a descendant widget. Call 'context.dependOnInheritedWidgetOfExactType' only after the InheritedWidget has been inserted into the tree, e.g., in a child widget's build method.
   ```
3. **Use a Provider package (like Riverpod) that manages InheritedWidgets automatically, ensuring proper placement.** (75% success)
   ```
   Use a Provider package (like Riverpod) that manages InheritedWidgets automatically, ensuring proper placement.
   ```

## Dead Ends

- **Replace const key with a non-const key using a random value** — The issue is not the const qualifier but the InheritedWidget's absence; changing the key doesn't fix the missing ancestor. (80% fail)
- **Wrap the entire app in a MaterialApp to provide default InheritedWidgets** — MaterialApp provides MediaQuery and Theme, but the error is about a custom InheritedWidget; MaterialApp won't supply it. (60% fail)
- **Add a dummy InheritedWidget at the root with a const key** — The dummy widget doesn't provide the required data; the accessing widget still fails to find the correct InheritedWidget. (90% fail)
