MAA
MAA

Reputation: 1355

Did I even use state management - notifyListeners() does not do anything

I have a simple app that takes two text fields and shows them in another page.

This is the first class:

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => Login(),
      child: MaterialApp(
        color: Colors.teal,
        home: App(),
      )
    );
  }
}

Then in the App class I have this:

@override
  Widget build(BuildContext context) {
    Login log = Provider.of<Login>(context);
.
.
.
.
// textfields that take email and password inputs
.
.
                FlatButton(
                  color: Colors.white,
                  child: Text('Save', style: GoogleFonts.pacifico()),
                  onPressed: () {
                    log.saveUser(  // THIS METHOD
                        User(emailController.text, passwordController.text));
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => ShowUser(),
                      ),
                    );
                  },
                )

So that button press will do two things:

  1. save the User object in the Login class implemented with ChangeNotifier.

The Login class is as follows:

class Login with ChangeNotifier {



 User currentUser;

  void saveUser(User user) {
    currentUser = user;

    // notifyListeners();  THIS LINE
  }

  User getUser() {
    return currentUser;
  }
}
  1. The button press will also navigate to a second page that has the following code:

    Login login = Provider.of<Login>(context);
    User _user = login.getUser();
    

Then I use the _user instance to show the email and password.

The code works and my inputs show up in the second page. The issue here is it works whether notifyListeners() is commented out or not.

Am I even applying state management here? Why isn't the notifyListeners method required?

enter image description here

Upvotes: 0

Views: 469

Answers (1)

Aman Malhotra
Aman Malhotra

Reputation: 3196

So how provider model works is:

  1. It renders the design of a particular widget (in your case App() widget) with the data that you have initially in the ChangeNotifer class (in your case Login class).
  2. If there occurs a change in any value inside ChangeNotifier class and you know that this change will affect the design, Eg- Lets say on a button press u increment a counter variable which u are showing in the widget. So you know if value of counter variable changes, the design has to update. If this is the case i.e design needs to be updated with the value, than you call notifyListeners() and it'll update the design corresponding to the new values.
  3. If you know that the change wont affect your design you dont need to call notifyListeneres() which in your case is true because you set the current user and immediately push to a new screen so updating the design of previous screen doesnt make any sense and even if you do call notifyListeners() it wont matter because by then u'll already be on the new screen.

So that is what is happening in your case the notifyListeners() line that you have commented will not do anything noticable and is kind of useless there.

BUT IT DOES NOT MEAN THAT YOU ARE NOT USING STATE MANAGEMENT, YOU ARE USING IT JUST NOT UPDATING THE DESIGN AFTER THE CHANGES IN DATA

Upvotes: 1

Related Questions