Home > database >  Exception caught by gesture: type '_RegExpMatch' is not a subtype of type 'double
Exception caught by gesture: type '_RegExpMatch' is not a subtype of type 'double

Time:07-23

Im trying to make a calculator to handle Ohm's Law and Power Law on Dart with Flutter. For context, V means Electric Voltage, I represents Amperage, R is for Resistance and P means Electric power, while O and P on circular brackets represents if the equation is for calculating on Ohm or Power law.

if (expression.contains("(V)") &&
            expression.contains("(I)") &&
            ohm) {
          expression = equation.replaceAll("(I)", "/");
          expression = expression.replaceAll("(V)", "/");
          expression = expression.replaceFirst("/", "First");
          expression = expression.replaceAll("/", "Second");
          RegExp fir = RegExp("^[^F]*");
          RegExp sec = RegExp("(?<=t)(.*?)(?=S)");
          double pri = 0;
          double seg = 0;
          print(expression);
          print(fir);
          print(sec);
          pri = fir.firstMatch(expression) as double;
          //Exception triggers here
          print(pri);
          seg = sec.firstMatch(expression) as double;
          print(seg);
          if (seg > pri) {
            expression = expression.replaceFirst(fir, seg as String);
            expression = expression.replaceFirst(sec, pri as String);
          }
          expression = expression.replaceFirst("First", "/");
          expression = expression.replaceFirst("Second", "");
          print(expression);
        }

In the code above, i'm trying to check if the second value is bigger than the first value, with Regex, in which i change the numbers from both sides with replace so it doesn`t trigger an error on the whole ordeal when trying to make the math. Like this example below.

4(V)8(I)

(code above)

expected result: 8 / 4

But for some reason, it keeps triggering the expection on the title and refuses to continue the code any further. It has something to do with Regex string not wanting to become a double, yes, but i can't find any answers online. Plus my online friends don't code with Dart which made me create this account and ask a question here.

  • Related