如何覆盖 State class 以便我可以访问 StatefulWidget 实例?

How to override State class so I could access StatefulWidget instance?

所以我有 2 个 classes:

class A extends StatefulWidget {
  A({Key? key}) : super(key: key);

  @override
  State<A> createState() => _AState();
}

class _AState extends State<A> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }

  void func(){}
}

我想覆盖 _AState classes func() 方法。所以我这样做:

class B extends A{
  final item = 10;

  @override
  State<A> createState() => _BState();
}

class _BState extends _AState{
  @override
  void func() {
    widget.item //can't do this
  }
}

我重写 func() 方法没问题,但现在我还需要访问我的新变量 item,它在 B class 中声明。我知道我不能那样做,因为实例 widget 是由 State<A> class.

提供的

所以我的问题是:How to access the variable item from B class in _BState?

将小部件投射到 B 对象

class _BState extends _AState{
  @override
  void func() {
    // (widget as B).item 
  }
}