2

I want to switch between the login screen and Home screen based on bool value(user.status) from the model class below

class User extends ChangeNotifier {
  int phoneNumber;
  bool status = false;
  notifyListeners();
}

The bool User.status value is flipped from below function

User _user = Provider.of<User>(context);
...
...
if (form.validate()) {

      _user.status = true;
}

The below function has to listen to the changes in the status value from the User model and change the screen to Home().

class Wrapper extends StatelessWidget {
@override
  Widget build(BuildContext context) {
    User authStatus = Provider.of<User>(context);
    return authStatus.status ? Home() : Auth();
  }
}

I don't have any errors, all the values are updating accordingly but the Wrapper() is not being rebuilt after listening to the changes from ChangeNotifier

  • you would want to use a streamProvider and not changenotifierprovider. Take a look at this: https://www.youtube.com/watch?v=j_SJ7XmT2MM . Don't forget to click on the github link in the description. – CoderUni Aug 18 '20 at 12:44
  • Yeah I followed the tutorial, But how to frame stream provider for a class like above, I have tried to stream the bool value but it is giving error (bool value can not be streamed), and what is wrong with change notifier. – saipavankumar muppalaneni Aug 18 '20 at 12:52
  • by using a stream, you are listening for any changes in the bool value. a changednotifier will only notify that the bool has changed only if you told it to do so. In short, you have to manually tell changednotifier to notify that the bool value has changed. – CoderUni Aug 18 '20 at 12:55
  • Okay, but I am getting an error when building stream around bool User.status. – saipavankumar muppalaneni Aug 18 '20 at 13:08
  • Make your wrapper stateful, it listens to the boolean but cannot rebuild since it doesn't have a state. Should work. – Arsh Shaikh Aug 18 '20 at 14:03

1 Answers1

0

Here's how I do it with Provider :

routes: {
  "/": (context) => MainPage(),
  "/detail": (context) => UserDetailPage(),
},
builder: (context, child) {
  return Consumer<UsersProvider>(
    child: child,
    builder: (context, provider, child) {

      final value = provider.user;
      if (!value.status) {
        return Navigator(
          onGenerateRoute: (settings) => MaterialPageRoute(
              settings: settings, builder: (context) => LoginPage()),
        );
      }

      return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) => UsersProvider()),
          ChangeNotifierProvider(
              create: (context) => InvoicesProvider()),
          ChangeNotifierProvider(create: (context) => EventsProvider()),
        ],
        child: child,
      );
    },
  );
},

Basically use builder in main.dart and defines routes, then inside builder use Consumer were child is the initial route MainPage() so if the user already login they will go there, and if not, base on status they will redirect to LoginPage(). I hope you can understand feel free to comment

Aldy Yuan
  • 1,795
  • 9
  • 23