I have a multi page form that I need to manage state with. I am currently trying to use the scoped model but it's not working. Everything I go to another page the state is cleared. I'm usually the PageView widget to create the multi page form. What I really what do know is why the state is not persisting. Thank you
Asked
Active
Viewed 517 times
1 Answers
0
You want to make sure your ScopedModel is correctly wrapping all of the pages in your multi page form. Often, you want to wrap your entire app with your ScopedModel. Something like this:
Future startUp() async {
UserModel userModel = await loadUser();
runApp(ScopedModel<UserModel>(model: userModel, child: MyApp()));
}
void main() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
startUp();
}
In some situations, you want to rebuild whenever the Model changes (users logs in?)
Example:
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<UserModel>(
builder: (BuildContext context, Widget child, UserModel model) {
return Scaffold(
body: ListView(
children: <Widget>[
// your page here
]
)
);
});
}

Tiago Rossi
- 156
- 8
-
I already fixed the problem by doing something like what you did on line two when calling the model as against calling the model directly as I was doing before. However I do like the other things you added. – Ghost Apr 26 '20 at 17:03