Flutter problem with finding provider context Flutter problem with finding provider context dart dart

Flutter problem with finding provider context


The issue is:

Your ChangeNotifierProvider is located inside Home, but you are trying to access it outside Home.

Providers are scoped. Which means that if it's located inside a widget tree, only its descendants can access it. As such, in your code, only Home can read from the provider.

To fix that, move the provider above MaterialApp:

ChangeNotifierProvider<UserRepository> (  builder: (context) => UserRepository(),  child: MaterialApp(    home: Home(),  ),)


You first need to create the Provider and place in the tree above the usage.for example, in your case:

  Widget build(BuildContext context) {final title = 'Tap to select';return MaterialApp(    title: title,    home: Scaffold(        appBar: AppBar(          title: Text(title),        ),        body: Provider<UserRepository> (          builder: (context) => UserRepository(),            dispose: (context, val) => val.dispose(),            child: NewRouteBody())    ));

}