3

I'm trying to handle my app's screens moments after boot.

Expected Routes would be:

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.

A Function handles this routing mechanism on boot being ran by MaterialApp home property.

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

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

But instead it returns a Assertion related to Overlay Entry.

Error:

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).
Brenno Fagundes
  • 461
  • 2
  • 5
  • 11

1 Answers1

4

You're trying to give the home property a StreamBuilder that will actually provide navigator calls instead of a Widget itself. What you want is return a PrecisoSplashScreen() or PrecisoLoginScreen() and so on.

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

Also, you may want to consider using a FutureBuilder widget.

Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87