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

Flutter中Object类型无法使用*运算符的报错求助

解决Flutter TweenAnimationBuilder乘法报错问题

报错原因:Tween未指定泛型类型,导致builder回调中的value被推断为Object类型,而Object没有定义*运算符,因此无法执行乘法操作。

两种解决方法:

方法1:指定泛型类型(推荐)

给TweenAnimationBuilder和Tween都加上<double>泛型,让value自动推断为double类型,无需强制转换:

body: Center(
  child: TweenAnimationBuilder<double>(
    tween: Tween<double>(begin: 0.0, end: 1.0),
    duration: Duration(seconds: 4),
    builder: (context, value, child){
      int percentage = (value * 100).ceil();
      return Container(
        width: size,
        height: size,
        child: Stack(
          children: [
            ShaderMask(
              shaderCallback: (rect){
                return SweepGradient(
                    startAngle: 0.0,
                    endAngle: TWO_PI,
                    stops: const [0.0,0.5],
                    center: Alignment.center,
                    colors: [Colors.blue,Colors.grey.withAlpha(55)]
                ).createShader(rect);
              },
              child: Container(
                width: size,
                height: size,
                decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    image: DecorationImage(image: Image.asset("assets/images/radial_scale.png").image)
                ),
              ),
            ),
            // 补充剩余组件代码
          ],
        ),
      );
    },
  ),
),

方法2:强制类型转换

如果不想修改泛型,直接在使用value时将其强转为double:

int percentage = ((value! as double) * 100).ceil();

方法1更符合Dart的类型安全规范,能避免后续潜在的类型问题,优先推荐使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 19:45:56