title: Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null
body: <p>Because of the following error, my app is stuck at HandleMainScreen. It Navigated properly for the first time but gives error in the console but when I hot restart it Navigation doesn't work and app stuck at HandleMainScreen.</p>
  
  <pre><code>E/flutter ( 8785): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null.
  E/flutter ( 8785): Receiver: null
  E/flutter ( 8785): Tried calling: findAncestorStateOfType&lt;NavigatorState&gt;()
  E/flutter ( 8785): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
  E/flutter ( 8785): #1      Navigator.of 
  package:flutter/…/widgets/navigator.dart:2115
  E/flutter ( 8785): #2      _HandleMainScreenState.checkAuthentication.&lt;anonymous closure&gt; 
  package:monktree/main.dart:58
  E/flutter ( 8785): #3      _rootRunUnary  (dart:async/zone.dart:1192:38)
  E/flutter ( 8785): #4      _CustomZone.runUnary  (dart:async/zone.dart:1085:19)
  E/flutter ( 8785): #5      _CustomZone.runUnaryGuarded  (dart:async/zone.dart:987:7)
  E/flutter ( 8785): #6      _BufferingStreamSubscription._sendData  (dart:async/stream_impl.dart:339:11)
  E/flutter ( 8785): #7      _BufferingStreamSubscription._add  (dart:async/stream_impl.dart:266:7)
  
  </code></pre>
  
  <p>Following code is what I am using. If you have any better way to handle user long kindly answer it on this question.</p>
  
  <pre><code>class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        builder: (context, child) {
          return ScrollConfiguration(
            behavior: MyBehavior(),
            child: child,
          );
        },
        home: HandleMainScreen(),
        debugShowCheckedModeBanner: false,
      );
    }
  }
  class MyBehavior extends ScrollBehavior {
    @override
    Widget buildViewportChrome(
        BuildContext context, Widget child, AxisDirection axisDirection) {
      return child;
    }
  }
  class HandleMainScreen extends StatefulWidget {
    @override
    _HandleMainScreenState createState() =&gt; _HandleMainScreenState();
  }
  
  class _HandleMainScreenState extends State&lt;HandleMainScreen&gt; {
    final FirebaseAuth _auth = FirebaseAuth.instance;
    bool isLoggedIn;
    FirebaseUser _user;
    String error;
  
    @override
    void initState() {
      super.initState();
      checkAuthentication();
    }
  
    checkAuthentication() async {
      _auth.onAuthStateChanged.listen((user) {
        _user = user;
        if (user == null) {
          Navigator.of(context).pushReplacement(
              MaterialPageRoute(builder: (context) =&gt; LoginPage()));
        } else if (user != null) {
          Navigator.of(context).pushReplacement(
              MaterialPageRoute(builder: (context) =&gt; HomePage(user)));
        }
      }).onError((e) {
        error = e.toString();
        print("Error in HandleMainScreen\n" + e.toString());
      });
    }
  
    @override
    Widget build(BuildContext context) {
      Constants.allocateSize(size: MediaQuery.of(context).size);
      return Scaffold(
        body: Center(
          child: CircularProgressIndicator(),
        ),
      );
    }
  }
  
  </code></pre>