请考虑以下代码段:
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变量保持不变。谁能解释我这种行为的原因?