2

I need push new screen automatic on app startup (if user is login or sign out).

I am use scoped_model for auth so need navigate when user value is change in model.

I am follow Brian Egan suggestion here: https://github.com/brianegan/scoped_model/issues/43#issuecomment-442444143

class LoginScreenState extends State<LoginScreen> {

  @override
  void didChangeDependencies() {
    ScopedModel.of<AuthModel>(context).addListener(_navigationListener);
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    ScopedModel.of<AuthModel>(context)
        .removeListener(_navigationListener);
    super.dispose();
  }


  void _navigationListener() {

switch (ScopedModel.of<AuthModel>(context).AuthStatus) {
  case AuthStatus.NotAuth:
    Navigator.of(context).pushNamed(‘/Login’);
    break;
  case AuthStatus.Auth:
    Navigator.of(context).pushNamed(‘/Main’);
    break;
  case AuthStatus.Register:
    Navigator.of(context).pushNamed(‘/Register’);
    break;

  }

AuthStatus is Enum. I change value in Model.

This is push route correct, but have issue:

Same route is push many times. For example, same Login page is push at least 5 times.

How to stop Navigator from push same screen multiple times?

Thanks!

FlutterFirebase
  • 2,163
  • 6
  • 28
  • 60

1 Answers1

0

In Brian Egan's example he had a boolean test in the _navigationListener method. His comment was:

// This function will be run every time the model changes! We will use it to // check the navigate boolean. If it's set to true, we'll push a new screen! // // If not, we won't do anything.

So, a similar boolean needs to be used in your code to only navigate once despite how many times the method is called.

GrahamD
  • 2,952
  • 3
  • 15
  • 26