flutter runtime_error ai_generated true

FlutterError: 'const' key not found in InheritedWidget context

ID: flutter/const-key-not-found

Also available as: JSON · Markdown · 中文
75%Fix Rate
82%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Flutter 3.10.0 active
Flutter 3.19.0 active
Dart 3.0.0 active

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.

generic

中文

小部件使用 const 键访问 InheritedWidget,但该 InheritedWidget 不在访问小部件上方的 widget 树中,通常是由于缺少 provider 或构建上下文不正确。

Official Documentation

https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html

Workarounds

  1. 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.
    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. 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.
    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. 75% success Use a Provider package (like Riverpod) that manages InheritedWidgets automatically, ensuring proper placement.
    Use a Provider package (like Riverpod) that manages InheritedWidgets automatically, ensuring proper placement.

中文步骤

  1. 确保 InheritedWidget 位于访问小部件上方的 widget 树中。例如,使用 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;
      }
    然后在访问之前在 build 方法中使用 MyProvider。
  2. 验证用于访问 InheritedWidget 的 BuildContext 来自后代小部件。仅在 InheritedWidget 已插入树后调用 'context.dependOnInheritedWidgetOfExactType',例如在子小部件的 build 方法中。
  3. 使用 Provider 包(如 Riverpod)自动管理 InheritedWidget,确保正确放置。

Dead Ends

Common approaches that don't work:

  1. Replace const key with a non-const key using a random value 80% fail

    The issue is not the const qualifier but the InheritedWidget's absence; changing the key doesn't fix the missing ancestor.

  2. Wrap the entire app in a MaterialApp to provide default InheritedWidgets 60% fail

    MaterialApp provides MediaQuery and Theme, but the error is about a custom InheritedWidget; MaterialApp won't supply it.

  3. Add a dummy InheritedWidget at the root with a const key 90% fail

    The dummy widget doesn't provide the required data; the accessing widget still fails to find the correct InheritedWidget.