Flutter problem with finding provider context

harunB10 picture harunB10 · Jul 28, 2019 · Viewed 15.8k times · Source

I have a problem with Flutter Provider pattern. After user is redirected to a new screen, the provider could not be found.

Following my previous question (Could not find the correct provider above this widget) I wrote this code:

class NewRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Tap to select';
    return MaterialApp(
        title: title,
        home: Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          body: NewRouteBody()
        ));
  }
}

class NewRouteBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var user = Provider.of<UserRepository>(context);
    return ListView(...)

I did same thing but I get again the error which says that it could not find the correct provider above this widget (NewRouteBody).

Tried to fix it somehow, Googled the answer for a few hours but without success...

Any help is appreciated.

EDIT

This is UserRepository which contains pattern:

class UserRepository with ChangeNotifier {
  User user;
  Status _status = Status.Uninitialized;

  Status get status => _status;
  User get getUser => user;
...}

EDIT 2:

Code snippet with ChangeNotifier:

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserRepository>(
      builder: (context) => UserRepository.instance(),
      child: Consumer<UserRepository>(
        builder: (context, UserRepository userRepository, _) {
          switch (userRepository.status) {
            case Status.Uninitialized:
              return Login();
            case Status.Unauthenticated:
              return Login();
            case Status.Authenticating:
            case Status.Authenticated:

              if(userRepository.getUser.isPrefSet == 0){
                return Selection();
              }
              return Dashboard();
          }
        },
      ),
    );
  }
}

Answer

R&#233;mi Rousselet picture Rémi Rousselet · Jul 29, 2019

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(),
  ),
)