Switch widget will properly work with these. But if I uncomment that section, Switch widget will freeze.
I need to set Switch initial value based on arguments condition. How to do that?
class BookingPageState extends State<BookingPage> {
bool reqPickup = false;
Widget build(BuildContext context) {
/* priceData = ModalRoute.of(context)?.settings.arguments;
if(priceData) setState(() {reqPickup = true; }); */
return Switch.adaptive(
value: reqPickup,
onChanged:(bool value) {
setState(() {reqPickup = value; });
}
)
}
}
2
Answers
The problem is how do you set the initial data of your switch because every time that you set a new state the build method executes again and set your variable with the screen args
You can solve this using the initState method of your statefulwidget like this
Here you go. I believe this is what you want. It is the minimum viable code, under 30 lines. I hope it works. Feel free to provide acknowledgement in the comment below.