i am new in Dart and Flutter. Is there an easy way to calculate an user input as a String like '3+5/8? Of course the result should be double-type. Thanks for your answers!
-
1This is a pretty broad question - the best way to proceed might be to do some research by yourself (there's a lot of existing material, like [How to evaluate a math expression given in string form](https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form)), and come back to Stack Overflow if you get stuck after choosing which approach to take :) – MyStackRunnethOver Feb 06 '19 at 01:05
-
Thank you very much for your help and the link. Of course i have an idea to code this "manually". I was just curious, if something already exist in an easy way. For sure i will do more research in deeper way. And yes, i can comment then my conclusions here – Aembe Feb 06 '19 at 11:40
3 Answers
If you are looking for simple mathematical strings, you calculate them with the help of a package called
First install this package. Add it into your project and then implement this code.
import 'dart:math';
import 'package:petitparser/petitparser.dart';
Parser buildParser() {
final builder = ExpressionBuilder();
builder.group()
..primitive((pattern('+-').optional() &
digit().plus() &
(char('.') & digit().plus()).optional() &
(pattern('eE') & pattern('+-').optional() & digit().plus())
.optional())
.flatten('number expected')
.trim()
.map(num.tryParse))
..wrapper(
char('(').trim(), char(')').trim(), (left, value, right) => value);
builder.group()..prefix(char('-').trim(), (op, a) => -a);
builder.group()..right(char('^').trim(), (a, op, b) => pow(a, b));
builder.group()
..left(char('*').trim(), (a, op, b) => a * b)
..left(char('/').trim(), (a, op, b) => a / b);
builder.group()
..left(char('+').trim(), (a, op, b) => a + b)
..left(char('-').trim(), (a, op, b) => a - b);
return builder.build().end();
}
double calcString(String text) {
final parser = buildParser();
final input = text;
final result = parser.parse(input);
if (result.isSuccess)
return result.value.toDouble();
else
return double.parse(text);
}
Now just call this function calcString and you will get the required answer. Remember it will only return the calculated value if the given string is valid else it will throw an error.

- 105
- 5
I think what you are looking for is a package that can do "expression evaluation". Searching on the pub site for "expression" yields a few results that look promising.
I don't have any direct experience with these packages so I can't recommend one.

- 10,145
- 2
- 26
- 22
-
Thank you very much for your comment! I will look at the links and go deeper in this topic now! – Aembe Feb 06 '19 at 20:51
I just programmed the Shunting-yard algorithm in Dart, problem solved =) Visit: https://en.wikipedia.org/wiki/Shunting-yard_algorithm

- 51
- 1
- 3
-
Hello, I know this post is old but I was wondering if you were able to share an example of this as I am stuck with this problem myself? – kiwikodes Jul 14 '19 at 03:38