由于以下错误,我的应用程序停留在HandleMainScreen。第一次可以正确导航,但是在控制台中显示错误,但是当我热重启时,导航无法正常工作,并且应用程序卡在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)
以下代码是我正在使用的代码。如果您有更好的方式来长时间处理用户,请对此问题进行解答。
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(),
),
);
}
}
答案 0 :(得分:2)
更改此:
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());
});
}
对此:
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());
});
}