搜索插件、代码仓与 APP 分类

插件详情v1.2.0

eval_ex

一个数学和布尔表达式求值器,移植自 https://github.com/uklimaschewski/EvalEx。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3460
喜欢数
27
Pub 分数
145

安装插件

flutter pub add eval_ex

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

EvalEx 是一个轻量级的 Dart 包,可在运行时评估数学和布尔表达式。它支持基本算术、括号、变量以及常见的函数(如 sin、cos 和 log)。最初是从 JavaScript 库移植而来,提供了一个简单的 API 来安全地解析和计算表达式。非常适合需要动态公式求值的应用程序,例如计算器、金融工具或基于配置的逻辑。该包可在所有主要平台(包括移动、Web 和桌面)上运行。

适合场景

  • 动态公式计算
  • 计算器应用
  • 基于配置的逻辑
  • 财务建模
  • 数据验证
  • 科学计算

主要特点

  • 支持算术和布尔表达式
  • 变量替换
  • 内置数学函数
  • 安全的表达式解析
  • 跨平台兼容性
  • 简单的 API,设置简单

适合对象

  • 开发计算器的开发者
  • 需要运行时表达式求值的应用程序
  • 教育工具
  • 后端逻辑,包含用户自定义公式
  • 原型化动态计算

使用建议

  • 不适合高度复杂或性能关键的数学运算
  • 不支持自定义函数定义
  • 表达式必须在语法上有效
  • 仅限基本操作和标准函数
  • 使用不受信任输入时的安全警告

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

eval_ex

A Dart port of https://github.com/uklimaschewski/EvalEx.
EvalEx supports mathematical and boolean expression evaluation, using arbitrary precision. EvalEx supports functions, variables, and operators and additionally makes it very easy to add your own custom functions or operators.

EvalEx uses decimal to support arbitrary precision arithmetic.
View package on pub.dev.

Basic example

void main() {
  Expression exp = Expression("2 ^ 4 + 8");
  print(exp.eval().toString()); // 24

  // With a variable
  exp = Expression("a ^ b + c");
  // Variables may contain alphanumeric characters, and "_". This can be changed
  // by using setVariableCharacters(..) and setFirstVariableCharacters(..) (what chars variable are allowed to start with).
  exp.setStringVariable("a", "2");
  exp.setStringVariable("b", "4");
  exp.setStringVariable("c", "8");
  print(exp.eval().toString()); // 24
  
  // With a function
  exp = Expression("MAX(-7,8)");
  print(exp.eval().toString()); // 8

  // Boolean logic
  exp = Expression("1>0 && 5 == 5");
  print(exp.eval().toString()); // 1
  
  exp = Expression("1>0 && 5 == 4");
  print(exp.eval().toString()); // 0
}
 

Built-in functions and operators

Function Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
^ Power
&& Returns 1 if both expressions are true, 0 otherwise
|| Returns 1 if either or both expressions are true, 0 otherwise
> Returns 1 if the left expression is greater than the right
>= Returns 1 if the left expression is greater than or equal to the right
< Returns 1 if the right expression is greater than the left
<= Returns 1 if the right expression is greater than or equal to the left
= Returns 1 if the left and right expressions are equal
== Returns 1 if the left and right expressions are equal
!= Returns 1 if the left and right expressions are NOT equal
<> Returns 1 if the left and right expressions are NOT equal
STREQ("str1","str2") Returns 1 if the literal "str1" is equal to "str2", otherwise returns 0
FACT(int) Computes the factorial of arg1
NOT(expression) Returns 1 if arg1 evaluates to 0, otherwise returns 0
IF(cond,exp1,exp2) Returns exp1 if cond evaluates to 1, otherwise returns exp2
Random() Returns a random decimal between 0 and 1
SINR(exp) Evaluates the SIN of exp, assuming exp is in radians
COSR(exp) Evaluates the COS of exp, assuming exp is in radians
TANR(exp) Evaluates the TAN of exp, assuming exp is in radians
COTR(exp) Evaluates the COT of exp, assuming exp is in radians
SECR(exp) Evaluates the SEC of exp, assuming exp is in radians
CSCR(exp) Evaluates the CSC of exp, assuming exp is in radians
SIN(exp) Evaluates the SIN of exp, assuming exp is in degrees
COS(exp) Evaluates the COS of exp, assuming exp is in degrees
TAN(exp) Evaluates the TAN of exp, assuming exp is in degrees
COT(exp) Evaluates the COT of exp, assuming exp is in degrees
SEC(exp) Evaluates the SEC of exp, assuming exp is in degrees
CSC(exp) Evaluates the CSC of exp, assuming exp is in degrees
ASINR(exp) Evaluates the ARCSIN of exp, assuming exp is in radians
ACOSR(exp) Evaluates the ARCCOS of exp, assuming exp is in radians
ATANR(exp) Evaluates the ARCTAN of exp, assuming exp is in radians
ACOTR(exp) Evaluates the ARCCOT of exp, assuming exp is in radians
ATAN2R(exp1, exp1) Evaluates the ARCTAN between exp1 and exp2, assuming exp1 and exp2 are in radians
ASIN(exp) Evaluates the ARCSIN of exp, assuming exp is in degrees
ACOS(exp) Evaluates the ARCCOS of exp, assuming exp is in degrees
ATAN(exp) Evaluates the ARCTAN of exp, assuming exp is in degrees
ACOT(exp) Evaluates the ARCCOT of exp, assuming exp is in degrees
ATAN2(exp1, exp1) Evaluates the ARCTAN between exp1 and exp2, assuming exp1 and exp2 are in degrees
RAD(deg) Converts deg to radians
DEG(rad) Converts rad to degrees
MAX(a,b,...) Returns the maximum value from the provided list of 1 or more expressions
MIN(a,b,...) Returns the minimum value from the provided list of 1 or more expressions
ABS(exp) Returns the absolute value of exp
LOG(exp) Returns the natural logarithm of exp
LOG10(exp) Returns the log base 10 of exp
ROUND(exp,precision) Returns exp rounded to precision decimal points
FLOOR(exp) Returns the floor of exp
CEILING(exp) Returns the ceiling of exp
SQRT(exp) Computes the square root of exp
e Euler's number
PI Ratio of circle's circumference to diameter
NULL Alias for null
TRUE or true Alias for 1
FALSE or false Alias for 0

Adding custom functions, operators, and variables

Custom functions, operators, and variables can be defined by adding them in built_ins.dart

Differences from https://github.com/uklimaschewski/EvalEx.

  • Hyperbolic functions aren't supported
  • The SQRT function is currently limited to 64-bit precision
  • Built in operators/functions/variables are instead defined in built_ins.dart

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。