【问题标题】:How to use a getter in another class?如何在另一个类中使用吸气剂?
【发布时间】:2021-06-03 17:16:13
【问题描述】:

在我的班级Tabelle我交出了一个头衔。现在我想在_TabelleState 类中输出一个Text(this.title)。但我收到错误“没有为类型 _TabelleState 定义 getter 标题”

如何在 _TabelleState 类中使用 this.title?

class Tabelle extends StatefulWidget {
  Tabelle({Key key, this.title}) : super(key: key);
  final String title;

  
  @override
  _TabelleState createState() => _TabelleState();
}

class _TabelleState extends State<Tabelle> {

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text(this.title),

【问题讨论】:

    标签: flutter class title getter


    【解决方案1】:

    对于有状态小部件,您可以从其状态访问关联的小部件:

    class Tabelle extends StatefulWidget {
      Tabelle({Key key, this.title}) : super(key: key);
      final String title;
    
      
      @override
      _TabelleState createState() => _TabelleState();
    }
    
    class _TabelleState extends State<Tabelle> {
    
    @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
              title: Text(widget.title),// Here is the magic ?
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 2018-04-05
      • 2021-03-01
      • 2019-09-27
      • 2012-01-31
      • 1970-01-01
      • 2019-11-26
      相关资源
      最近更新 更多