First of, this might probably just be a misunderstanding, but let's see.
In my App's build() function, I'm wrapping my MaterialApp with a BlocProvider:
return BlocProvider<AuthenticationBloc>(
create: (context) {
final appConfig = AppConfig.of(context);
return AuthenticationBloc(
userRepository: UserRepository(
clientId: appConfig.oauth2ClientId,
discoveryUrl: '${appConfig.oauth2Authority}/.well-known/openid-configuration',
),
)..add(AppStarted());
},
child: MaterialApp(
routes: {
StartPage.routeName: (context) => StartPage(),
AboutPage.routeName: (context) => AboutPage(),
},
initialRoute: StartPage.routeName,
),
);
In each page, I pass the page contents to a generic Scaffold widget which builds a drawer. In that drawer I have a button for signing in:
BlocProvider.of<AuthenticationBloc>(context).add(LoggingIn());
The problem now is that the of() method returns null in this case, and I just don't understand why:
The following NoSuchMethodError was thrown while handling a gesture:
The method 'add' was called on null.
Receiver: null
Tried calling: add(Instance of 'LoggingIn')
Hi @DASPRiD ๐
Thanks for opening an issue!
Are you able to share a sample app which reproduces this issue? Thanks ๐
Not exactly sure what you'd exactly require for that โ I'm pretty new to Flutter development.
I guess I could share my current application with you to take a look at (it's pretty much just scaffolding right now, no functionality behind it) โ do you have a GitLab account I can invite?
Yeah sure, you can add @felangel on Gitlab ๐
There you go :)
@DASPRiD took a look and you just need to update your AppConfig to:
static AppConfig of(BuildContext context) {
return context.getElementForInheritedWidgetOfExactType<AppConfig>().widget;
}
Hope that helps ๐
Huh okay, guess that happens when you use tutorials from all over the internet and try to combine them, thanks! :) โ Got an explanation, why? :)
So I updated my code here, still getting the null error though.
What are the steps to get the null error. The reason why is because getElementForInheritedWidgetOfExactType just performs the lookup for the nearest InheritedWidget whereas dependOnInheritedWidgetOfExactType establishes a relationship and triggers rebuilds which also makes it impossible to lookup within initState. The way BlocProvider works is it looks up the dependencies within initState so you should avoid using dependOnInheritedWidgetOfExactType because it's also O(n) whereas getElementForInheritedWidgetOfExactType is O(1).
What are the steps to get the null error.
Open the drawer and click the "sign in" button.
(Also, the state parameter is apparently also always null)
hmm I'm not able to reproduce ๐ค
Weird, nevermind. I had to restart the app, for some reason the hot reload did not influence the behaviour :)
Really great library by the way, coming from a React background, this feels a lot like React Hooks :)
Thanks I really appreciate it! ๐
Most helpful comment
Really great library by the way, coming from a React background, this feels a lot like React Hooks :)