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

Flutter中出现_TypeError (int不是double?的子类型)错误求助

解决Flutter中_TypeError (type 'int' is not a subtype of type 'double?')错误

这个错误的核心原因是BorderRadius.circular()要求传入double类型参数,但你传入了int类型,再加上自定义函数的参数未指定类型(默认是dynamic),导致Dart无法自动完成类型转换,触发类型不匹配。

两种解决方式:

方式一:给自定义函数参数指定明确类型(推荐)

在Dart中明确参数类型能从源头避免这类类型问题,修改后的代码如下:

import 'package:flutter/material.dart';

Widget CustomButton(double height, double width, double radius, String buttonText, {Color? color}) {
  return Container(
    height: height,
    width: width,
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(radius),
      color: color,
      border: Border.all(color: Colors.grey)
    ),
    child: Center(child: Text(buttonText)), // 给文本加居中,优化按钮显示效果
  );
}

修改点说明:

  • 给height、width、radius指定double类型,buttonText指定String类型,color指定Color?(可选的Color类型)
  • 明确函数返回值为Widget,符合Flutter组件规范

调用时确保传入参数类型匹配,示例:

CustomButton(50.0, 200.0, 10.0, "点击按钮", color: Colors.blue)

方式二:在函数内部将int类型的radius转为double

如果调用时确实需要传入int值(比如10),可以在BorderRadius.circular()里做类型转换:

BorderRadius.circular(radius.toDouble())

但这种方式不如方式一严谨,建议优先使用方式一明确参数类型。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 16:05:38