avoid_ js_ rounded_ ints
Details about the 'avoid_js_rounded_ints' diagnostic produced by the Dart analyzer.
Integer literal can't be represented exactly when compiled to JavaScript.
Description
#
The analyzer produces this diagnostic when an int literal's value
can't be represented exactly as a JavaScript Number.
When Dart code is compiled to JavaScript,
integers whose values fall outside the range -(2^53 - 1) to 2^53 - 1
might be rounded to the nearest representable value.
This silently changes the literal's value on the web.
To learn more about how Dart represents numbers on different platforms, including when compiled to JavaScript, check out Numbers in Dart.
Example
#
The following code produces this diagnostic because
the literal 9007199254740993 (2^53 + 1) can't be
represented exactly as a JavaScript Number is rounded to
9007199254740992 at run time when compiled to JavaScript:
final i = 9007199254740993;
Common fixes
#
If the value needs to be exact on all platforms,
including when compiled to JavaScript,
then use a BigInt to represent it:
final i = BigInt.parse('9007199254740993');
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Report an issue.