Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null

本秂侑毒 提交于 2020-06-29 04:23:42

问题


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.

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<NavigatorState>()
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.<anonymous closure> 
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)

Following code is what I am using. If you have any better way to handle user long kindly answer it on this question.

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

class _HandleMainScreenState extends State<HandleMainScreen> {
  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) => LoginPage()));
      } else if (user != null) {
        Navigator.of(context).pushReplacement(
            MaterialPageRoute(builder: (context) => 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(),
      ),
    );
  }
}


回答1:


Change this:

  checkAuthentication() async {
    _auth.onAuthStateChanged.listen((user) {
      _user = user;
      if (user == null) {
        Navigator.of(context).pushReplacement(
            MaterialPageRoute(builder: (context) => LoginPage()));
      } else if (user != null) {
        Navigator.of(context).pushReplacement(
            MaterialPageRoute(builder: (context) => HomePage(user)));
      }
    }).onError((e) {
      error = e.toString();
      print("Error in HandleMainScreen\n" + e.toString());
    });
  }

into this:

  checkAuthentication() async {
    _auth.onAuthStateChanged.listen((user) {
      _user = user;
      if (user == null) {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => LoginPage()),
        );
      } else if (user != null) {
         Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => HomePage(user)),
        );
      }
    }).onError((e) {
      error = e.toString();
      print("Error in HandleMainScreen\n" + e.toString());
    });
  }


来源:https://stackoverflow.com/questions/62281488/unhandled-exception-nosuchmethoderror-the-method-findancestorstateoftype-was

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!