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

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


Solution 1:[1]

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
        }
        
      }
    },
  ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1