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.
>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.