當我單擊該按鈕時,我需要將其名稱添加到該按鈕,List并且當再次單擊該按鈕時,它的名稱將添加到該按鈕List,但如果串列中的 2 個名稱匹配,那么它們都需要被洗掉。我試圖通過 for 回圈來實作,但它對我來說不能正常作業。告訴我如何做到這一點?
功能
List<String> types = [];
void reportButton(int number, String type) {
reportsList[number] = !reportsList[number];
if (reportsList.contains(true)) {
isEnabledC = true;
} else {
isEnabledC = false;
}
types.add(type);
for (var element in types) {
if (element == type) {
types.removeWhere((element) => element == type);
}
}
紐扣
SizedBox(
width: size.width / 3,
child: GestureDetector(
onTap: () {
cubit.reportButton(0, 'comment');
// state.type = 'comment';
},
child: ReportButton(
enabled: state.reports[0],
svg: constants.Assets.comment,
text: 'Comment',
),
),
),
SizedBox(
width: size.width / 3,
child: GestureDetector(
onTap: () {
cubit.reportButton(1, 'broken');
// state.type = 'broken';
},
child: ReportButton(
enabled: state.reports[1],
svg: constants.Assets.attention,
text: 'Broken station',
),
),
),
uj5u.com熱心網友回復:
您不必遍歷字串串列。您可以簡單地檢查串列是否包含字串。如果是,洗掉它,否則添加字串
List<String> types = ["apple", "ball", "cat", "dog"];
void main() async {
reportButton(1, "xyz");
}
void reportButton(int number, String type) {
if (types.contains(type)) {
types.removeWhere((element) => element == type);
} else {
types.add(type);
}
print(types);
}
uj5u.com熱心網友回復:
List<String> types = ["boy", "girl", "father", "hog"];
void main() {
reportButton(1, "xyz");
}
void reportButton(int number, String type) {
if (types.contains(type)) {
types.removeWhere((element) => element == type);
}else {
types.add(type);
}
print(types);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/491015.html
上一篇:我如何解決這個問題?
