8

Widget Hierarchy

I’m invoking the of function of the InheritedWidget, but my function is returning null when, as you can see above, I have my widget at the top of the tree. The call is coming from a page that was pushed onto the Navigator stack, which is not this page. Anyone know why? My InheritedWidget code is below.

class LiveKHProvider extends InheritedWidget {
  final LiveKHBloc liveKHBloc = LiveKHBloc();

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) => oldWidget != this;

  static LiveKHBloc of(BuildContext context) {
    var inheritFromWidgetOfExactType =
        context.inheritFromWidgetOfExactType(LiveKHProvider); // to clearly see what’s returning null. 
        //This is where it returns null, 
        //so the below line is executed on a null object.
    return (inheritFromWidgetOfExactType as LiveKHProvider).liveKHBloc;
  }

  LiveKHProvider({Key key, Widget child}) : super(key: key, child: child);
}
5
  • How are you using context? Is it actually a context from below the inherited widget, or is it a root context? Commented Aug 7, 2018 at 1:25
  • The context of the calling widget inside the build function. Are you asking about the use of context inside the inherited widget or inside the calling widget? Commented Aug 7, 2018 at 1:55
  • And I just debugged it and found that it does run through the constructor of the object I'm trying to grab, meaning it gets instantiated, but for some reason, I'm not getting access to it Commented Aug 7, 2018 at 3:15
  • In which file is the above InheritedWidget code located, @ThinkDigital? Is it in your main.dart file? Commented Aug 9, 2018 at 22:36
  • No, it's in a separate class file by itself. Commented Aug 9, 2018 at 23:46

2 Answers 2

3

InheritedWidget, in my testing atleast, can be inherited even if it's a parent of the MaterialApp/WidgetsApp/CupertinoApp or route.

For anyone else having this problem, a more likely explaination is that you've imported the InheritedWidget with different casing. i.e. Where you generate it, you may have imported it as import 'MyInheritedWidget.dart', while where you are trying to retrieve it, you have it imported as import 'myinheritedwidget.dart' .

This wont cause an error in either file (in windows atleast), but it will not be matched and return null.

Sign up to request clarification or add additional context in comments.

6 Comments

That would cause an error with the analyzer and wouldn't run
ran for me. Both on android and ios. Unless you're trying to get it too early in the widget lifecycle, in which case it just becomes a race condition no matter what you do or where you put the widget.
You can import a file with the wrong casing and it works?
yep, imported, ran, but then crashed once i tried to use it as an inherited widget.
Totally crazy, fixed my problem - its wild that the tooling doesn't catch that for you
|
0

I solved the issue by wrapping the page that was passed to my MaterialPageRoute in the InheritedWidget. I'm assuming that the tree stops at the page route and doesn't reach higher, although I'm not 100% sure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.