Reputation: 17
I'm trying to create a switch that will either add or remove a value from a list.
I've created my stateful widget and a boolean variable for my switch:
class CustomisePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _CustomisePageState();
}
}
class _CustomisePageState extends State<CustomisePage> {
List<String> names = ["Name 1", "Name 2", "Name 3", "Name 4"];
bool isSwitched = true;
I've created my switch with a conditional statement checks whether the switch is turned "on", and the list doesn't contain the value "Name 1", then adds the value to the list names
. If the switch is turned "off", it removes "Name 1" from the list.
SwitchListTile(
activeColor: Colors.amber,
value: isSwitched,
onChanged: (value) {
setState(() {
isSwitched = value;
if (isSwitched = true && names.contains("Name 1") = false) {
names.add("Name 1");
} else {
names.remove("Name 1");
}
});
},
title: Text("Name 1"),
secondary: const Icon(Icons.add),
)
This issue however is that Flutter/Dart doesn't like "games.contains("Name 1") = false" statement I've created to check if the value is in the list.
I would really appreciate any help, or even advice on improving my code to make it more efficient!
Upvotes: 1
Views: 1183
Reputation: 336
As mentioned before using single = is for assigning a value, it is not comparable operator if you want to check equality you must use this == operator.
Upvotes: 0
Reputation: 10789
try isSwitched == true && names.contains("Name 1") == false
or better isSwitched && !names.contains("Name 1")
Using a single =
is an assignment which cannot be done here
Upvotes: 2