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

Flutter应用开发中NoSuchMethodError: 'text'异常修复求助

修复NoSuchMethodError: 'text'异常

问题原因

自定义按钮ourButton的title参数未声明具体类型,被推断为dynamic类型。Dart的扩展方法(即你项目中使用的String.text)无法在dynamic类型上直接调用,导致找不到text方法抛出异常。同时原代码中按钮点击回调未正确执行,仅引用了onPress变量而非调用它。

修复代码

修改自定义按钮ourButton的实现,给参数指定明确类型并修正回调逻辑:

import 'package:dma_inc/consts/consts.dart';
import 'package:flutter/material.dart';

Widget ourButton(VoidCallback onPress, Color color, Color textcolor, String title) {
  return ElevatedButton(
      style: ElevatedButton.styleFrom(
        backgroundColor: color,
        padding: const EdgeInsets.all(12),
      ),
      onPressed: onPress,
      child: title.text.color(textcolor).fontFamily(bold).make());
}

关键说明

  • 给title指定String类型后,Dart能正确识别并调用对应的text扩展方法,解决异常问题。
  • 直接将onPress传递给onPressed参数,确保按钮点击时能触发回调逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 05:43:10