Reputation: 1355
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:
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;
}
}
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?
Upvotes: 0
Views: 469
Reputation: 3196
So how provider model works is:
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