子路由弹出时,Flutter SharedPreference会更新

时间:2018-12-07 20:27:27

标签: routes dart flutter sharedpreferences

我有点扑朔迷离,我开始研究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中进行更改时,在返回主路由后,由于initState在运行时未运行,因此SharedPreference不变您会从堆栈中弹出一条路由。

void initState() {
        SharedPreference().getLightsOn().then(updateLight);
        super.initState();
    }
    void updateLight(bool lt) {
        setState(() {
            _lights = lt;
        });
    }

我的解决方法是将其放入构建小部件中以获取值,但我认为这不是一个好方法,因为它会一遍又一遍地循环。

Widget build( BuildContext context ) {
  SharedPreference().getLightsOn().then(updateLight);
  ...

1 个答案:

答案 0 :(得分:0)

当您推送新的Page / var fixture = new Fixture() .Compose<NoBar>() .Compose<NoFoo>() .Compose(); var result = fixture.Create<MyClass>(); // All the properties should be set as normal by autofixture except for both Foo and Bar which should be omitted. public class NoBar : ICustomization { public void Customize(IFixture fixture) { fixture.Customize<MyClass>(c => c.Without(r => r.Bar)); } } public class NoFoo : ICustomization { public void Customize(IFixture fixture) { fixture.Customize<MyClass>(c => c.Without(r => r.Foo)); } } 时,可以使用Widget关键字在弹出页面后等待。像这样:

await