1

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

FelX3
  • 49
  • 5

1 Answers1

1

okay I tried properly with simulator.

change setState to this:

 WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {
      getValue("blueUri").then((value) => {
        _blueUriInit = value,
      });
    }));
dfassf
  • 142
  • 10
  • Did not help, sorry – FelX3 Jan 24 '23 at 14:03
  • I have figured my problem, but I cannot understand why? Future getValue(String key) async { final prefs = await SharedPreferences.getInstance(); String value = prefs.getString(key) ?? "NULL"; return value; } always return NULL – FelX3 Jan 25 '23 at 09:07
  • refer to this post https://stackoverflow.com/questions/48844804/flutter-setstate-not-updating-inner-stateful-widget/63307118#63307118 basically pure setState is called before the widget is built, so the value doesn't appear in the widget even though it is actually loaded – dfassf Jan 25 '23 at 13:18