1

Consider the following code snippet:

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();
  }
}

In the above code for every time the build method of _CustomStateFulWidget called, the navigatorKeyFinal variable value changes, but the navigatorKeyLocal variable stay unchanged. Can anyone explain me the reason of this behaviour?

Md Golam Rahman Tushar
  • 2,175
  • 1
  • 14
  • 29

0 Answers0