英文:
How to exit if else and for loop in Flutter?
问题
我计划在Flutter中退出if else或循环语句,当检测到列表中存在重复项时。但是,当我在if和else语句中放置break
时,它并不起作用良好。如果我在if中放置break
,那么即使if语句中有break
退出循环,else语句仍将继续执行。如果我在else语句中放置break
,那么由于else语句中的break
会影响i++
,if语句将根本不执行。而如果我在if和else语句中都放置break
,它会显示我的i++
是死代码。
以下是代码:
void addFavourite() {
favouriteCountry.add(properties[0]['name']);
}
void checkDuplicates() {
int length = favouriteCountry.length;
String cName = properties[0]['name'];
if (length > 0) {
for (int i = 0; i < length; i++) {
String name = favouriteCountry[i];
print(name);
if (name == cName) {
print(length);
showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Duplicated action, country already added to list.'),
content: SingleChildScrollView(
child: ListBody(
children: const <Widget>[
Text('You really liked this country huh?'),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
break;
} else {
addFavourite();
displayAddFavourite();
print("object");
}
}
} else {
addFavourite();
displayAddFavourite();
print("object1");
}
}
英文:
I am planning to exit my if else or loop statement in Flutter when it detects there are duplicated item in my lists. But it does not work well when I put break
in the if and else statement. When I put break
in if, then the else statement will continue to execute even when the if statement had break
the loop. If I put break
in the else statement, then the if statement will not execute at all since the break
in else statement will affect i++
. While if I put break
in both if and else statement, it shows my i++
is dead code.
Here are the code:
void addFavourite() {
favouriteCountry.add(properties[0]['name']);
}
void checkDuplicates() {
int length = favouriteCountry.length;
String cName = properties[0]['name'];
if (length > 0) {
for (int i = 0; i < length; i++) {
String name = favouriteCountry[i];
print(name);
if (name == cName) {
print(length);
showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text(
'Duplicated action, country already added to list.'),
content: SingleChildScrollView(
child: ListBody(
children: const <Widget>[
Text('You really liked this country huh?'),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
break;
} else {
addFavourite();
displayAddFavourite();
print("object");
}
}
} else {
addFavourite();
displayAddFavourite();
print("object1");
}
}
I tried to put i++ in both if and else statement, but it does not work. So if there is anything I can add into my code to make it work. Please feel free to enlighten me. Thanks in advance!
答案1
得分: 0
你可以考虑使用一个 `flag` 并相应地显示 `dialog`。
英文:
You can probably consider using a flag
and show the dialog
accordingly.
void checkDuplicates() {
int length = favouriteCountry.length;
String cName = properties[0]['name'];
bool foundDuplicate = false; // set this flag
for (int i = 0; i < length; i++) {
String name = favouriteCountry[i];
print(name);
if (name == cName) {
foundDuplicate = true;
break;
}
}
if (foundDuplicate) {
showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
// ...
},
);
} else {
addFavourite();
displayAddFavourite();
print("object");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论