我有点慌张,我开始使用SharedPreference . 一切都像魅力一样,但当我在子路线上进行更改时,父路线不会更新 .
我使用MaterialApp路由参数:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter app',
home: Counter(),
routes: <String, WidgetBuilder>{
'/settings': (BuildContext context) => new Settings(),
});
}
}
我有启动应用程序时使用initState的父路由,但是当我打开一个新页面然后在SharedPreference中进行更改时,我回到主路径后,SharedPreference没有改变,因为当你弹出一个时,initState没有运行路线形成堆栈 .
void initState() {
SharedPreference().getLightsOn().then(updateLight);
super.initState();
}
void updateLight(bool lt) {
setState(() {
_lights = lt;
});
}
我的解决方法是将它放在构建小部件中以获取值,但我认为这不是好方法,因为它一遍又一遍地循环 .
Widget build( BuildContext context ) {
SharedPreference().getLightsOn().then(updateLight);
...
1 回答
当您按下新的Page /
Widget
时,可以在弹出页面后使用await
关键字等待 . 像这样 :