Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘

简介: Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘

出现场景

服务端返回的金额数据可能是整数,也可能是小数。

无论我们按int或double来解析,都可能出错。

如果我们定义的类型是int,返回的是double就会报以下异常。

int money = data["money"];
type 'int' is not a subtype of type 'double'

如果我们定义的类型是double,返回的是int就会报以下异常。

double money = data["money"];
type 'double' is not a subtype of type 'int'

解决方案

Dart不像JavaScript一样能自动转换,需要我们按具体类型处理。

声明num(推荐)

使用num类型来接收服务端返回的值。

/// 会自动转换成对应的类型
num money = data["money"];

num源码

part of dart.core;

/// 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`.
///
/// **See also:**
/// * [int]: An integer number.
/// * [double]: A double-precision floating point number.
/// * [Numbers](https://dart.dev/guides/language/numbers) in
/// [A tour of the Dart language](https://dart.dev/guides/language/language-tour).

abstract class num implements Comparable<num> {
  ...
}

查看num源码得知,num是一个int整形或者double浮点型,会自动根据右边赋值的类型决定自身的类型。

判断类型

这种方式稍微麻烦一点,根据返回的不同类型做不同的处理。

声明类型为double的方式

double money = 0.0;
var result = data["money"];
if(result is int){
  money = result.toDouble();
} else{
  money = result;
}

声明类型为int的处理方式

int money = 0;
var result = data["money"];
if(result is int){
  money = result;
} else{
  money = result.toInt();
}
相关文章
|
2月前
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
64 1
|
1月前
|
Dart
如何在 Flutter 项目中使用 Dart 语言?
如何在 Flutter 项目中使用 Dart 语言?
120 58
|
11天前
|
Dart 开发者 Windows
flutter:dart的学习
本文介绍了Dart语言的下载方法及基本使用,包括在Windows系统上和VSCode中的安装步骤,并展示了如何运行Dart代码。此外,还详细说明了Dart的基础语法、构造函数、泛型以及库的使用方法。文中通过示例代码解释了闭包、运算符等概念,并介绍了Dart的新特性如非空断言操作符和延迟初始化变量。最后,提供了添加第三方库依赖的方法。
24 12
|
4天前
|
存储 C语言
使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小
【10月更文挑战第13天】使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小。
22 1
|
19天前
|
Python
[oeasy]python036_数据类型有什么用_type_类型_int_str_查看帮助
本文回顾了Python中`ord()`和`chr()`函数的使用方法,强调了这两个函数互为逆运算:`ord()`通过字符找到对应的序号,`chr()`则通过序号找到对应的字符。文章详细解释了函数参数类型的重要性,即`ord()`需要字符串类型参数,而`chr()`需要整数类型参数。若参数类型错误,则会引发`TypeError`。此外,还介绍了如何使用`type()`函数查询参数类型,并通过示例展示了如何正确使用`ord()`和`chr()`进行转换。最后,强调了在函数调用时正确传递参数类型的重要性。
19 3
|
2月前
|
Dart 前端开发 JavaScript
Flutter&Dart-异步编程Future、Stream极速入门
Flutter&Dart-异步编程Future、Stream极速入门
68 4
Flutter&Dart-异步编程Future、Stream极速入门
|
2月前
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
36 4
|
2月前
|
Dart 开发工具 Android开发
Android Studio导入Flutter项目提示Dart SDK is not configured
Android Studio导入Flutter项目提示Dart SDK is not configured
207 4
|
2月前
|
Dart 开发工具
解决升级Flutter3.0后出现警告Operand of null-aware operation ‘!‘ has type ‘WidgetsBinding‘ which excludes null
解决升级Flutter3.0后出现警告Operand of null-aware operation ‘!‘ has type ‘WidgetsBinding‘ which excludes null
33 1
|
2月前
|
Dart 安全 API
Android跨平台开发之Dart 3.5 与 Flutter 3.24:革新跨平台应用开发
【Dart 3.5 与 Flutter 3.24:革新跨平台应用开发】首发于公众号“AntDream”。本文深度解析 Dart 3.5 和 Flutter 3.24 的新特性,包括空安全强化、Web 与原生互操作性增强及 Flutter GPU API 等,展示了如何提升代码质量和用户体验。
59 1