This method is not updating the widget
Widget displayAppropriateWidget() {
if (isParsedCorrectly) {
_widgetToDisplay = displaySecondHalf;
} else {
_widgetToDisplay = displayFirstHalf;
}
setState(() {});
return _widgetToDisplay;
}
Even when the bool in other file and this file updates widget doesn't update. I can only see the changes only after I hot reload
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const PreferredSize(
preferredSize: Size.fromHeight(100),
child: CustomAppBar("Sign-Up"),
),
body: Align(
alignment: Alignment.center,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.only(top: 100, bottom: 5),
child: Image.asset(
'assets/images/college.png',
width: 200,
fit: BoxFit.cover,
),
),
displayAppropriateWidget(),
],
),
),
);
}
}
You need to listen to the changes of that
boolby injecting it into theWidgetin some way. For example by:boolis declared in aWidgethigher up in the widget tree. Then you can callsetStatein that parentWidgetonce you've updated thebool. ButsetStatecan only be used inStatefulWidget. More on usingsetStatehere: https://docs.flutter.dev/development/ui/interactive and here: https://api.flutter.dev/flutter/widgets/State/setState.htmlAlso, your function
displayAppropriateWidgetcallssetState, which you should not do from inside a Widgetsbuildmethod - since that would mean rebuilding every time you build, causing infinite rebuilds.Another tip is to try to avoid building Widgets from functions, as it's not as performant and can lead to some hard to identify bugs: https://www.youtube.com/watch?v=IOyq-eTRhvo