Flutter: when move to new page shared_prefernce from some reason stop working

48 Views Asked by At

When back to BlueSettings page, initState show null on "Current value:"

When use stand alone BlueSettings whithout any MaterialPageRoute, it works

I want to use with MaterialPageRoute, what I do I miss?

Thanks for your help

here is my code:


main.dart

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Application'),
      ),
      drawer: const DrawerMenu(),
    );
  }
}

drawer_menu.dart

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

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        // Important: Remove any padding from the ListView.
        padding: EdgeInsets.zero,
        children: <Widget>[
          ListTile(
            title: const Text("Blue settings"),
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => const BlueSettings(),
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}

blue_settings.dart enter image description here

1

There are 1 best solutions below

3
On

okay I tried properly with simulator.

change setState to this:

 WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
      getValue("blueUri").then((value) => {
        _blueUriInit = value,
      });
    }));