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方法
}
相關文章
相關標籤/搜索