avoid_js_rounded_ints
避免 JavaScript 四舍五入的整数。
详情
#避免 在编译为 JavaScript 时无法精确表示的整数字面量。
当程序编译为 JavaScript 时,int 和 double 会变成 JavaScript Numbers。过大的整数(value < Number.MIN_SAFE_INTEGER 或 value > Number.MAX_SAFE_INTEGER)可能会四舍五入到最接近的 Number 值。
例如,1000000000000000001 无法精确表示为 JavaScript Number,因此将使用 1000000000000000000 代替。
错误示例
dart
int value = 9007199254740995;
正确示例
dart
BigInt value = BigInt.parse('9007199254740995');
启用
#要启用 avoid_js_rounded_ints
规则,请在您的 analysis_options.yaml
文件中的 linter > rules 下添加 avoid_js_rounded_ints
。
analysis_options.yaml
yaml
linter:
rules:
- avoid_js_rounded_ints
如果您改用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 avoid_js_rounded_ints: true
。
analysis_options.yaml
yaml
linter:
rules:
avoid_js_rounded_ints: true
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。