Search code examples
flutterif-statementbreak

How to exit if else and for loop in Flutter?


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!


Solution

  • 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");
      }
    }