Exception caught by gesture: type '_RegExpMatch' is not a subtype of type 'double' in type cast in Flutter/Dart

Viewed 44

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.

Accepting all answers and advice if you have any!

And if there any questions about my code, i'll be happy to answer.

2 Answers

You are trying to cast the result of the call to fir.firstMatch(expression) to a double, but fir.firstMatch(expression) returns a RegExpMatch, which is not something dart knows how to turn into a double. You can however get the String value of the match by retrieving the appropriate group from the match, and then parse the double from the string. Potentially something like

val match = fir.firstMatch(expression);
val doubleAsString = match.group(0);
val pri = double.parse(doubleAsString);

Here is the documentation for dart's RegExpMatch class: https://api.dart.dev/stable/2.17.6/dart-core/RegExpMatch-class.html

After some time brainstroming with some online friends me and them have found the solution!

Using matchString to get the subtring i want like in example i mentioned earlier, and then converting this String using parse for the literal number to a double, i can get the values i want out of the String! For some reasson JavaScript needs to do this part of the logic instead of Dart and because as can't do literal values with Regex.

And then converting this double back to a String i can use toString() method for the literal definition of the numbers and use replace() to replace them.

The code above resolved should look like this:

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;
          pri = double.parse(fir.stringMatch(expression)!);
          seg = double.parse(sec.stringMatch(expression)!);
          if (seg > pri) {
            expression = expression.replaceFirst(fir, seg.toString());
            expression = expression.replaceFirst(sec, pri.toString());
          }
          expression = expression.replaceFirst("First", "/");
          expression = expression.replaceFirst("Second", "");
        }

Notice that in the code i also have the exclamation mark so it can expect a null value. It's also required to run set code.

So, the code now runs in this order:

expression: 4(V)8(I)

4/8/

4First8Second

"Regex logic"

8First4Second

8/4Second

8/4

Hope this helps anyone who had or will have the same problem i have! Take care!

Related