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.
Discussions

How to convert a double to an int in Dart? - Stack Overflow
The following produces the below error: int calc_ranks(ranks) { double multiplier = .5; return multiplier * ranks; } The return type double is not a int, as defined by the method calc_ranks. H... More on stackoverflow.com
🌐 stackoverflow.com
parsing - How do I parse a string into a number with Dart? - Stack Overflow
If the default value is 0 you can ... into a numeric value. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... Release notes and bug fixes for beta.stackoverflow.com · 38 How to extract number only from string in flutter... More on stackoverflow.com
🌐 stackoverflow.com
num VS int and double
num is just base type of int and double. I would suggest using int or double whenever possible cause it's considered more type safe than using base types, because you might need to cast them to int or double to consume them. It's like you can use Object for every value in dart (except null). But you'll end up casting them to required types anyways and it implies less type safety, so we instead use types accordingly instead of base types. More on reddit.com
🌐 r/dartlang
10
11
November 16, 2021
dart - Why can't I convert a Number into a Double? - Stack Overflow
As a side note, Dart compiled to ... type, which means that all numbers are doubles. In JS compiled code, all integers can be assigned to double without conversion. That will not work when the code is run on a non-JS implementation, like Flutter, Dart VM/server or ahead-of-time ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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
July 5, 2024 - Learn the methods to convert a double to an int in Dart with this comprehensive guide. Step-by-step instructions are included.
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
Flutteradda
flutteradda.com › flutter › double-to-int-flutter.html
Double to int in flutter - FlutterAdda
March 23, 2024 - 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().
🌐
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.
🌐
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
dart.dev › guides › language › numbers
Numbers in Dart
July 2, 2024 - 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.
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.