在状态变量未更改时更改Flutter有状态窗口小部件的最终窗口小部件变量的原因

时间:2020-05-21 19:36:42

标签: flutter dart statefulwidget

请考虑以下代码段:

class CustomStateFulWidget extends StatefulWidget {
  final GlobalKey<NavigatorState> navigatorKeyFinal =  GlobalKey();

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

class _CustomStateFulWidget extends State<CustomStateFulWidget> {
  GlobalKey<NavigatorState> navigatorKeyLocal = GlobalKey();

  @override
  Widget build(BuildContext context) {
    print("Navigator key local: ${navigatorKeyLocal.hashCode}");
    print("Navigator key final: ${widget.navigatorKeyFinal.hashCode}");

    return Center();
  }
}

在上面的代码中,每次调用 _CustomStateFulWidget build方法时,navigatorKeyFinal变量值都会更改,但是navigatorKeyLocal变量保持不变。谁能解释我这种行为的原因?

0 个答案:

没有答案