When to use Simple Expressions
Simple Expressions is a tiny, zero‑dependency library that lets you parse and evaluate mathematical and logical expressions at runtime. Whether you need to calculate a discount, filter a list based on a user‑entered rule, or power a custom calculator widget, this package turns a plain string like `"(price * quantity) > 100 && discount < 20"` into a boolean result in milliseconds. The core engine supports basic arithmetic, comparison operators, logical connectors, parentheses, and a handful of built‑in functions such as `max`, `min`, and `abs`. Because it has no external dependencies, it adds virtually no overhead to your app bundle and works equally well on mobile, web, and desktop platforms.
The library shines in scenarios where you want to expose dynamic calculations to end‑users without recompiling the app. For example, a finance app can let users define custom formulas for portfolio performance, a game can let designers tweak damage formulas via a simple text field, and an e‑commerce platform can store discount rules in a remote config and evaluate them on the fly. Since the evaluator runs entirely on the client, it eliminates the need for a backend service to interpret expressions, reducing latency and simplifying architecture.
Integrating Simple Expressions into a Flutter project is straightforward. After adding the package, you create an `Expression` instance with the raw string, optionally register custom variables or functions, and call `evaluate()` to get the result. The API is deliberately minimal: a single class with a clear, synchronous `evaluate` method, making it easy for beginners to adopt and for seasoned developers to embed in larger pipelines. The package also provides a small utility to pre‑compile expressions, which can be cached for repeated use, aligning well with typical storage‑and‑cache patterns in Flutter apps.
When using Simple Expressions in production, keep a few considerations in mind. Because the evaluator executes arbitrary strings, you should validate or sandbox user‑provided input to avoid unexpected behavior or performance spikes. The library does not support complex features like variable assignment, loops, or external I/O, which is a deliberate design choice to keep the runtime safe and deterministic. For high‑frequency calculations, pre‑compiling expressions and reusing the compiled objects is recommended to avoid repeated parsing overhead. Finally, the package targets Dart 2.12 and later, so it works with null‑safety enabled projects and all recent Flutter releases.
Overall, Simple Expressions offers a pragmatic balance between power and simplicity. Its tiny footprint, clear API, and lack of external dependencies make it an ideal choice for any Flutter app that needs on‑the‑fly calculations without pulling in heavyweight math libraries. Whether you are building a prototype, a learning app, or a production‑grade solution, this evaluator can be the backbone of dynamic rule engines, custom calculators, and user‑driven data transformations.
Pros
- tiny footprint
- no external dependencies
- supports basic math & logic
- works on all Flutter platforms
- easy to learn API
Watch outs
- no support for complex scripting features
- no built‑in sandboxing for untrusted input
- limited to predefined functions
Setup notes
Add the package to your project with: ``` flutter pub add simple_expressions ``` Then run `flutter pub get` to fetch the dependency. Import the library using `import 'package:simple_expressions/simple_expressions.dart';` and you are ready to start evaluating expressions.
Requires Dart SDK >=2.12.0 (null‑safety). Compatible with Flutter 2.0 and later on iOS, Android, web, macOS, Linux, and Windows. No additional platform‑specific setup is needed.
import 'package:simple_expressions/simple_expressions.dart';
void main() {
final expr = Expression('(price * quantity) > 100 && discount < 20');
final context = {
'price': 25,
'quantity': 5,
'discount': 15,
};
final result = expr.evaluate(context);
print('Expression result: $result'); // prints: Expression result: true
}