use_truncating_division
Use truncating division.
Description
#The analyzer produces this diagnostic when the result of dividing two numbers is converted to an integer using toInt
.
Dart has a built-in integer division operator that is both more efficient and more concise.
Example
#The following code produces this diagnostic because the result of dividing x
and y
is converted to an integer using toInt
:
dart
int divide(int x, int y) => (x / y).toInt();
Common fixes
#Use the integer division operator (~/
):
dart
int divide(int x, int y) => x ~/ y;
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.