Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type 'int' is not a subtype of type 'double?' #56913

Closed
dhilshana opened this issue Oct 18, 2024 · 3 comments
Closed

type 'int' is not a subtype of type 'double?' #56913

dhilshana opened this issue Oct 18, 2024 · 3 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior type-question A question about expected behavior or functionality

Comments

@dhilshana
Copy link

This usually happens when an API response or data processing step returns an int value, but my model or function is expecting a double?.

When a double variable is assigned an int value, implicit conversion from int to double is possible. However, the mentioned error occurs in this case.

@dart-github-bot
Copy link
Collaborator

Summary: The issue arises when an API response or data processing step returns an integer value, but the code expects a nullable double. While implicit conversion from int to double is possible, the code throws an error in this scenario.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Oct 18, 2024
@jasimrazi
Copy link

I'm experiencing the same problem. Can you suggest a solution?

@mraleph
Copy link
Member

mraleph commented Oct 18, 2024

Dart1 does not implicitly coerce int to double (or vice versa), because (among other reasons) such conversions are not loss-less. You have to explicitly convert using num.toDouble() or num.toInt().

Footnotes

  1. With the exception of JavaScript backend (dart2js) which uses JavaScript numbers to represent both int and double.

@mraleph mraleph closed this as completed Oct 18, 2024
@mraleph mraleph added closed-as-intended Closed as the reported issue is expected behavior type-question A question about expected behavior or functionality and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

4 participants