mercredi 20 janvier 2021

How to remove if-else chain from Dart

I have quite a huge if-else chain in a Flutter application which checks a bunch of conditions from a list of titles. I'm trying to figure out what the most effective way of dealing with such a chain would be. Does anyone have a suggestion if a Map would be suitable or maybe a simple swithcase? Thank You!

if (listOfMaps[i]["title"] == "BPM LOW" && !activeAlarms.contains("BPM LOW")) {
            activeAlarms.add("BPM LOW");
          } else if (listOfMaps[i]["title"] == "BPM HIGH" && !activeAlarms.contains("BPM HIGH")) {
            activeAlarms.add("BPM HIGH");
          } else if (listOfMaps[i]["title"] == "PIP LOW" &&
              !activeAlarms.contains("PIP LOW")) {
            activeAlarms.add("PIP LOW");
          } else if (listOfMaps[i]["title"] == "PIP HIGH" &&
              !activeAlarms.contains("PIP HIGH")) {
            activeAlarms.add("PIP HIGH");
          }

Aucun commentaire:

Enregistrer un commentaire