Flutter The getter 'XX' isn't defined for the type 'StatefulWidget'.

刚接触Flutter,踩坑中~
该报错提示的意思是没有访问到对象的相关属性。
排查问题常见的方法:

  • 检查该属性是否拼写错误
  • 检查该对象是否实现该属性
  • 检查该对象是否和你想访问的对象一致:特定情况下是需要指定对象类型的。下面有一个例子:
import 'package:flutter/material.dart';

class ListPage extends StatefulWidget {
  ListPage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _ListPageState createState() => _ListPageState();
}

class _ListPageState extends State {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        child: Text("Text"),
      ),
    );
  }
}

上面的代码中widget.title抛了如题的错误,最开始无从下手,最后才发现Statewidget并不知道widgettitle属性,所以需要指定State所对应的widget是哪一个类。
解决方法很简单:

image.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容