Flutter中的OverlayEntry在Navigator.push()上断言

时间:2018-12-17 14:38:44

标签: dart flutter

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

预期路线为:

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).

1 个答案:

答案 0 :(得分:3)

您正在尝试给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小部件。