我正在尝试在应用程序启动后处理屏幕。
预期的路由如下:
boot -> check if is logged in -> if yes -> navigator.push() to MainWindow.
boot -> check if is logged in -> no snapshot data -> navigator.push() to LoadingScreen.
boot -> check if is logged in -> if no -> navigator.push() to LoginScreen.
一个函数通过MaterialApp的home属性在启动时处理此路由机制。
main.dart:
Widget build(BuildContext context) {
return MaterialApp(
title: "Preciso Metrologia",
navigatorKey: navigatorKey,
theme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.lightBlue[400],
accentColor: Colors.deepPurple[400],
),
//home: LoginScreen()
home: handleCurrentScreen());
}
函数 handleCurrentScreen():
Widget handleCurrentScreen() {
return new StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => new PrecisoSplashScreen()));
} else {
if (snapshot.hasData) {
navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => new MainCertWindow(uuid: snapshot.data.uid)));
}
navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => new PrecisoLoginScreen()));
}
}
);
}
但实际上它返回了与覆盖层条目相关的断言。 错误:
I/flutter ( 4502): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4502): The following assertion was thrown building StreamBuilder<FirebaseUser>(dirty, state:
I/flutter ( 4502): _StreamBuilderBaseState<FirebaseUser, AsyncSnapshot<FirebaseUser>>#db480):
I/flutter ( 4502): setState() or markNeedsBuild() called during build.
I/flutter ( 4502): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter ( 4502): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter ( 4502): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter ( 4502): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter ( 4502): Otherwise, the framework might not visit this widget during this build phase.
I/flutter ( 4502): The widget on which setState() or markNeedsBuild() was called was:
I/flutter ( 4502): Overlay-[LabeledGlobalKey<OverlayState>#a2f7f](state: OverlayState#bbb92(entries:
I/flutter ( 4502): [OverlayEntry#3da4e(opaque: false; maintainState: false), OverlayEntry#87966(opaque: false;
I/flutter ( 4502): maintainState: true), OverlayEntry#d9c82(opaque: false; maintainState: false),
I/flutter ( 4502): OverlayEntry#388a4(opaque: false; maintainState: true)]))
I/flutter ( 4502): The widget which was currently being built when the offending call was made was:
I/flutter ( 4502): StreamBuilder<FirebaseUser>(dirty, state: _StreamBuilderBaseState<FirebaseUser,
I/flutter ( 4502): AsyncSnapshot<FirebaseUser>>#db480).
home
属性提供一个StreamBuilder
,它实际上会提供导航器调用而不是一个Widget本身。你想要的是返回一个PrecisoSplashScreen()
或PrecisoLoginScreen()
等等... - Miguel Ruivo