You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter开发报错:int类型无法转为double类型求助

Flutter类型错误:'int' is not a subtype of type 'double' 解决方法

错误详情

构建HomePage时抛出_TypeError:type 'int' is not a subtype of type 'double',调用栈指向custom_tab_indicator.dart第11行的RoundedRectangleTabIndicator构造函数,以及main.dart第173行的调用位置。

问题原因

你在TabBar的indicator参数中传入的RoundedRectangleTabIndicator构造参数(weight:4、width:24、padding:12)都是整数类型,但该类的对应参数定义为浮点类型(double)。Dart是强类型语言,不允许隐式类型转换,因此触发类型不匹配错误。

修复方案

方案1:将参数改为浮点值

直接在整数后加.0,显式将int转为double:

indicator: RoundedRectangleTabIndicator(
  color: kPrimaryColor, 
  weight: 4.0, 
  width: 24.0, 
  padding: 12.0
),

方案2:修改RoundedRectangleTabIndicator类(自定义类适用)

如果这个指示器类是你自己实现的,可以调整构造函数,兼容int类型输入:

class RoundedRectangleTabIndicator extends Decoration {
  final Color color;
  final double weight;
  final double width;
  final double padding;

  // 允许传入num类型(兼容int和double),内部自动转为double
  RoundedRectangleTabIndicator({
    required this.color,
    required num weight,
    required num width,
    required num padding,
  })  : weight = weight.toDouble(),
        width = width.toDouble(),
        padding = padding.toDouble();

  // 其他实现代码...
}

错误代码片段标注

你代码中触发错误的TabBar部分:

TabBar(
  labelPadding: const EdgeInsets.only( right: 24),
  indicatorWeight: 8,
  isScrollable: true,
  labelColor: kDarkColor,
  labelStyle: kJakartaBodyBold.copyWith(
    fontSize: SizeConfig.blockSizeHorizontal! * kBody1,
  ),
  unselectedLabelColor: kDark40Color,
  // 此处参数为int类型,需改为double
  indicator: RoundedRectangleTabIndicator(color: kPrimaryColor, weight: 4, width: 24, padding: 12),
  splashFactory: NoSplash.splashFactory,
  overlayColor: MaterialStateProperty.resolveWith((states) => states.contains(MaterialState.focused) ? null : Colors.transparent,),
  tabs: const [
    Tab(text: 'Primary',),
    Tab(text: 'Socia;',), // 注:此处存在拼写错误,建议修正为'Social'
    Tab(text: 'Forums',),
  ]
),

内容的提问来源于stack exchange,提问作者kcbibek43

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:20:09