gpt4 book ai didi

dart - Flutter 中 OverlayEntry 引起的 Navigator.push() 断言

转载 作者:IT老高 更新时间:2023-10-28 12:45:52 27 4
gpt4 key购买 nike

我正在尝试在启动后立即处理我的应用程序的屏幕。

预期路线为:

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 主属性运行的启动时处理这种路由机制。

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

但它会返回一个与 Overlay Entry 相关的断言。

错误:

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() 等等。

Widget handleCurrentScreen() {
return new StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return new PrecisoSplashScreen();
} else {
if (snapshot.hasData) {
return new MainCertWindow(uuid: snapshot.data.uid);
}
return new PrecisoLoginScreen();
}
}
);
}

此外,您可能需要考虑使用 FutureBuilder小部件。

关于dart - Flutter 中 OverlayEntry 引起的 Navigator.push() 断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53817422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com