Flutter 中如何退出 if-else 和 for 循环?

huangapple go评论112阅读模式
英文:

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][&#39;name&#39;]);
  }

  void checkDuplicates() {
    int length = favouriteCountry.length;
    String cName = properties[0][&#39;name&#39;];

    if (length &gt; 0) {
      for (int i = 0; i &lt; length; i++) {
        String name = favouriteCountry[i];
        print(name);
        if (name == cName) {
          print(length);
          showDialog&lt;void&gt;(
            context: context,
            barrierDismissible: false, // user must tap button!
            builder: (BuildContext context) {
              return AlertDialog(
                title: const Text(
                    &#39;Duplicated action, country already added to list.&#39;),
                content: SingleChildScrollView(
                  child: ListBody(
                    children: const &lt;Widget&gt;[
                      Text(&#39;You really liked this country huh?&#39;),
                    ],
                  ),
                ),
                actions: &lt;Widget&gt;[
                  TextButton(
                    child: const Text(&#39;OK&#39;),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            },
          );
          break;
        } else {
          addFavourite();
          displayAddFavourite();
          print(&quot;object&quot;);
        }
      }
    } else {
      addFavourite();
      displayAddFavourite();
      print(&quot;object1&quot;);
    }
  }

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][&#39;name&#39;];
  bool foundDuplicate = false;          // set this flag

  for (int i = 0; i &lt; length; i++) {
    String name = favouriteCountry[i];
    print(name);
    if (name == cName) {
      foundDuplicate = true;
      break;
    }
  }

  if (foundDuplicate) {
    showDialog&lt;void&gt;(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        // ...
      },
    );
  } else {
    addFavourite();
    displayAddFavourite();
    print(&quot;object&quot;);
  }
}

huangapple
  • 本文由 发表于 2023年3月12日 17:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定