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

Flutter新手求助:Undefined name 'title'错误解决

解决Flutter中Undefined name 'title'错误

问题原因

你在ListElement类的方法中直接使用title、desc时,编译器无法识别这是类的实例成员变量。Dart规范中,在类的方法内访问实例成员需要通过this.来明确引用,避免作用域混淆。

修复代码

将Text组件的调用代码修改为:

Text(this.title, style: TextStyle(fontWeight: FontWeight.w400, fontSize: 30)),
Text(this.desc, style: TextStyle(fontWeight: FontWeight.w400, fontSize: 15)),

额外验证点

如果修改后仍报错,检查以下内容:

  • 确认ListElement正确继承自StatelessWidget/StatefulWidget并实现build方法,完整示例:
    class ListElement extends StatelessWidget {
      final String title;
      final String desc;
      final String timer;
    
      const ListElement({Key? key, required this.title, required this.desc, required this.timer})
          : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            Text(this.title, style: TextStyle(fontWeight: FontWeight.w400, fontSize: 30)),
            Text(this.desc, style: TextStyle(fontWeight: FontWeight.w400, fontSize: 15)),
          ],
        );
      }
    }
    
  • 确保创建ListElement实例时,正确传入所有必填参数,比如:
    ListElement(title: "示例标题", desc: "示例描述", timer: "10:00")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 14:30:36