In essence, I have the switches created this way within an overridden build
's State
method:
List<Switch> daySwitches = [];
for (int i = 0; i <= 6; i++) {
daySwitches.add(Switch(
(...)
onChanged: (bool value) {
days[i].active = value;
},
(...)
value: days[i].active,
));
After that, the content of daySwitches
is added in athe build method, this way:
return Column(children: daySwitches);
The problem is that when I click on one Switch
, any of them, there all changed altogether. When I put a breakpoint in onChanged
, I see that the variable i
has the right value, but also see that all the active
fields of each day are really changed altogether during the execution of this only line of code.
Why does this happen? Is it because I use a temporary variable (daySwitches
)? Should I really use something like return Column(children: days.forEach(...).toList();
, or so?