Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"flutter" one setstate does not rerender on flutter web

Most basic setState function does not rerender my web app. Can anybody tell me what wrong with this setState code? Similar code worked fine for my android and ios app.

Widget build(BuildContext context) {
Map<String, dynamic> testMap = {"one": 5, "two": 3};
String wow = "wow";
return Scaffold(
  body: Column(
    children: [
      Text(testMap.toString()),
      Text(wow),
      ElevatedButton(
          onPressed: () {
            setState(() {
              testMap = SplayTreeMap<String, dynamic>.from(testMap, (k1, k2) => testMap[k1].compareTo(testMap[k2]));
              wow = "hey";
            });
            print(testMap.toString());
          },
          child: const Text("sort"))
    ],
  ),
);
}

My apologies in advance, I know this question has been asked many time but since I could not even find any issue with my code I needed help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you are using setState it will rebuild the widget which means build will be called again.

In the build method you are assigning the variables every time build gets executed thats why the value does not change.
You need to move that out of there.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading