Round it using the round() method:

int calc_ranks(ranks) {
    double multiplier = .5;
    return (multiplier * ranks).round();
}
Answer from user1804599 on Stack Overflow
🌐
Flutter
api.flutter.dev › flutter › dart-core › num › toInt.html
toInt method - num class - dart:core library - Dart API
API docs for the toInt method from the num class, for the Dart programming language.
🌐
Flutter
api.flutter.dev › flutter › dart-core › num-class.html
num class - dart:core library - Dart API
An integer or floating-point number. It is a compile-time error for any type other than int or double to attempt to extend or implement num.
🌐
Bacancy Technology
bacancytechnology.com › qanda › flutter › convert-a-double-to-an-int-in-dart
How to Convert a Double to an Int in Dart: A Complete Guide
Learn the methods to convert a double to an int in Dart with this comprehensive guide. Step-by-step instructions are included.
🌐
Medium
murtazasulaihi.medium.com › dive-into-dart-int-double-num-652c440bf427
Dive into Dart: 'int', 'double', 'num'. Numeric Flexibility in Dart by Murtaza Sulaihi | Medium
January 4, 2024 - Remember to handle exceptions when parsing, as it might fail if the string is not a valid number. I hope this explanation and examples help you understand the usage of ‘int’, ‘double’, and ‘num’ in Dart as a beginner. Through this Flutter Counter App, we’ll understand the use of ‘int’ for item counts, ‘double’ for precise prices, and ‘num’ for flexible total costs.
🌐
Flutter
api.flutter.dev › flutter › dart-core › int-class.html
int class - dart:core library - Dart API
Returns the sign of this integer. ... Available on num, provided by the NumToJSExtension extension Converts this num to a JSNumber.
🌐
KindaCode
kindacode.com › article › ways-to-convert-double-to-int-in-flutter-dart
4 ways to convert Double to Int in Flutter & Dart - KindaCode
This succinct, practical article walks you through 4 common ways to convert a double to an integer in Flutter and Dart. ... The toInt() method will truncate a double to an integer and return a result whose type is int. In other words, positive numbers will be rounded down (e.g.
Find elsewhere
🌐
DEV Community
dev.to › thphuc › dart-flutter-how-to-parse-string-to-number-j79
Dart/Flutter — How to parse String to number - DEV Community
November 7, 2020 - #dart #flutter #stringtonumber #nextfunc · In this post, let’s learn a few methods to parse/convert String to number (integer, double or both). /// Parse string to integer int stringToInt_parse(String input) { return int.parse(input, onError: (error) { // Return null if input string is invalid.
🌐
DEV Community
dev.to › wangonya › how-you-turn-a-string-into-a-number-or-vice-versa-with-dart-392h
Dart String to Int: How you turn a string into a number (or vice versa) with Dart - DEV Community
October 4, 2018 - // int -> String main () { String oneAsString = 1.toString(); print(oneAsString == '1'); // prints true } // double -> String main () { String piAsString = 3.14159.toStringAsFixed(2); print(piAsString == '3.14'); // prints true } I've been wanting to take up mobile app development with flutter so I thought I'd get comfortable with dart first before diving into flutter.
🌐
Flutteradda
flutteradda.com › flutter › double-to-int-flutter.html
Double to int in flutter
In Flutter, there are times when we need to convert a double value to an integer. For this purpose, we can use the built-in dart method toInt().
🌐
Flutter Stuff
flutterstuff.com › converting-strings-to-int-or-double-in-dart-flutter-a-comprehensive-guide
Converting Strings to Int or Double in Dart/Flutter: A Comprehensive Guide - Flutter Stuff
December 9, 2024 - The main difference between `int.parse()` and `double.parse()` is that `int.parse()` expects the string to represent an integer, whereas `double.parse()` expects the string to represent a number that can be a decimal. 5. Why is it essential to handle exception when converting strings to numbers? Handling exceptions is crucial when converting strings to numbers to prevent your app from crashing due to invalid inputs. This is particularly important in real-world applications where user input may be uncertain. ... Flutter Stuff Is A Team Of Passionate Flutter Developers On A Mission To Empower The Community.
🌐
Dart Tutorial
dart-tutorial.com › convert string to int › dart how to › dart tutorial - learn dart programming
Convert String to Int :: Dart Tutorial - Learn Dart Programming
In dart, you can convert String to int by using the int.parse() method. ... You can replace String with any numeric value and convert String to int. Make sure you have numeric value there.
🌐
Dart
dart.dev › guides › language › numbers
Numbers in Dart
In particular, Dart has two very different types of targets it compiles to: Native: Most often, a 64-bit mobile or desktop processor. Web: JavaScript as the primary execution engine. The following table shows how Dart numbers are usually implemented: For native targets, you can assume that int maps to a signed 64-bit integer representation and double maps to a 64-bit IEEE floating-point representation that matches the underlying processor.
🌐
Devsheet
devsheet.com › flutter-string-to-int
How to convert String to Int in Flutter - Devsheet
To convert a string to an integer in Flutter, you can use the int.parse() method or the int.tryParse() method.
Top answer
1 of 5
44

When parsing 100 from Firestore (which actually does not support a "number type", but converts it), it will by standard be parsed to an int.
Dart does not automatically "smartly" cast those types. In fact, you cannot cast an int to a double, which is the problem you are facing. If it were possible, your code would just work fine.

Parsing

Instead, you can parse it yourself:

double weight = json['weight'].toDouble();

Casting

What also works, is parsing the JSON to a num and then assigning it to a double, which will cast num to double.

double weight = json['weight'] as num;

This seems a bit odd at first and in fact the Dart Analysis tool (which is e.g. built in into the Dart plugin for VS Code and IntelliJ) will mark it as an "unnecessary cast", which it is not.

double a = 100; // this will not compile

double b = 100 as num; // this will compile, but is still marked as an "unnecessary cast"

double b = 100 as num compiles because num is the super class of double and Dart casts super to sub types even without explicit casts.
An explicit cast would be the follwing:

double a = 100 as double; // does not compile because int is not the super class of double

double b = (100 as num) as double; // compiles, you can also omit the double cast

Here is a nice read about "Types and casting in Dart".

Explanation

What happened to you is the following:

double weight;

weight = 100; // cannot compile because 100 is considered an int
// is the same as
weight = 100 as double; // which cannot work as I explained above
// Dart adds those casts automatically
2 of 5
12

You can do it in one line:

 double weight = (json['weight'] as num).toDouble();
🌐
Flutter
api.flutter.dev › flutter › dart-core › int › parse.html
parse method - int class - dart:core library - Dart API
If no radix is given then it defaults to 10. In this case, the source digits may also start with 0x, in which case the number is interpreted as a hexadecimal integer literal, When int is implemented by 64-bit signed integers, hexadecimal integer literals may represent values larger than 263, in which case the value is parsed as if it is an unsigned number, and the resulting value is the corresponding signed integer value.