How do I use Stream Builder to also replace the pushed widgets from Navigator?

Viewed 145

I am using a stream builder that reacts to the Firebase user state. I am in "Landing Screen", and I push "Login Screen" on top of it. After I log in, the Stream Builder replaces the "Landing Screen" with "Discover Screen" correctly. However, the pushed "Login Screen" remains on top of "Discover Screen".

How do I use Stream Builder to also replace the pushed widgets from Navigator?

Here is the code:

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final AuthService _authService = GetIt.I.get<AuthService>();
    return Scaffold(
      body: StreamBuilder(
        stream: _authService.userStream,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const LoadingScreen();
          } else if (snapshot.hasError) {
            return const ErrorScreen();
          } else if (snapshot.hasData) {
            return const DiscoverScreen();
          } else {
            return const LandingScreen(); // I push the Login Screen inside this screen
          }
        },
      ),
    );
  }
}
2 Answers

Can you show the code of how you push LoginScreen on top of LandingScreen? If you're using Navigator, you'll need to dismiss the LoginScreen so that you're rendering the HomeScreen which is returning the DiscoverScreen (view) inside it.

If that's the case, you can dismiss LoginScreen with Navigator.of(context).popUntil(ModalRoute.withName('/homescreen'));

Except replace the MediaRoute.withName('...') with the name of your HomeScreen route.

Alternatively, you can use the StreamBuilder to cover the state for when login should be showing.

you will try this code I think you should try this in your MaterialApp(home: here...) if you wanted to trying in your app in beginning time.

StreamBuilder(
    stream: _authService.userStream,
    builder: (context, snapshot) => 
authResultSnapshot.connectionState ==
                          ConnectionState.waiting
                      ? LoadingScreen : LandingScreen()
{
      if (snapshot.connectionState == ConnectionState.waiting) {
        return const LoadingScreen();
      } else if (snapshot.hasError) {
        return const ErrorScreen();
      } else if (snapshot.hasData) {
        return const DiscoverScreen();
      } else {
        if(snapshot.connectionState == ConnectionState.done){
          // your loginScreen();
        } else{
          return const LandingScreen(); // I push the Login Screen inside this screen
        }
        
      }
    },
  ),
Related