What is NaN in Flutter and what is caused by?
NaN could be caused by dividing by a variable which cannot be cast to a numbe as @JoSSte stated. But as I found out empirically, Flutter does not throw an error while dividing by 0, as opposed to python for example where ZeroDivisionError is thrown. Flutter sees the output of a division by zero as infinity or -infinity, when dividing respectively a positive and a negative number by zero.
So this makes sense, but in instances when infinity cannot be accepted as a value (as it isn't) it throws an error stating there was an error in the framework itself.
![Fabrizio](https://lh3.googleusercontent.com/-YeR2TZ3wNbE/AAAAAAAAAAI/AAAAAAAABFY/My-ET8irgeI/photo.jpg?sz=256)
Fabrizio
Updated on December 09, 2022Comments
-
Fabrizio over 1 year
When I run an app where an icon, wrapped in a Positioned is drawn on a container, I get an error stating that the offset, (Positioned's left) uses a NaN value. I took a look online and I found that NaN means Not a Number, but I couldn't find what causes it. I mean, If I divide a number by Zero I'll get an error saying that I cannot divide a number by zero, but NaN is a constant for what I understood, and if I was multiplying a number let's say for a string I'd get incompatible types error, instead of NaN. I'm not asking for an answer specific for my code as I couldn't provide it, but rather a more general explanation.
-
JoSSte over 5 yearsa NaN is most often because you are trying to divide by
"elephant"
ortrue
or something else which cannot be cast to a number... -
Fabrizio over 5 yearsthe problem is that I get the value which Flutter says it's NaN from a multiplication of a Random().nextDouble and a double value. So, nothing strange with types so far...
-
JoSSte over 5 yearshave you tried to print the output of your input to stdout to verify that it is indeed generating a number?
-
Fabrizio over 5 yearsI solved I was dividing by Zero at some point.
-
Tokenyet about 4 yearsA special case happened to me is to add
0.0
and-0.0
a bunch of time, the double go tonan
, I never know why, but add a condition to prevent value.abs from adding < 0.01.
-