Flutter中的this和this.widget的区别

  • this是指当前对象
  • this.widget是指当前组件
    比如我有一个有状态的组件ShapeStep,在_ShapeStepState中的this.widget才能访问到str属性,而this访问不到。
class ShapeStep extends StatefulWidget {
  final String str;

  const ShapeStep({Key key, this.autologousPreparation}) : super(key: key);

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

class _ShapeStepState extends State<ShapeStep> {

  @override
  void dispose() { // 声明周期函数——销毁时执行的方法
    print("aaa   " + this.toString());
    print("bbb    " + this.widget.toString());
    print("是否一样 " + (this == this.widget).toString()); // false
    this.widget.str= '张大哥';

    print("ShapeStep被销毁");
    super.dispose();
  }
  // 省略build方法
}

猜你喜欢

转载自www.cnblogs.com/novae/p/12380351.html
今日推荐