Home > Enterprise >  Flutter Navigator pushnamed object not working when sent to class as parameter
Flutter Navigator pushnamed object not working when sent to class as parameter

Time:11-25

I am trying to refactor my code and use a separate class object to register users and then send them to the correct page which is either the awaiting for email confirmation screen(if they haven't confirmed their email address) or the actual chat screen in the app.

I pass the Navigator.pushNamed object from the Flutter material build to the separate class object, but the Navigator.pushNamed doesn't work when it is sent as a parameter to the class object. It only worked when it is in the main build.

This is the main section that collects the parameters and sends them to the class

Widget build(BuildContext context) {...         



Registration(
                    email: email,
                    password: password,
                    awaitingEmailConfPage:
                        () async {
                      await Navigator.pushNamed(context, EmailConfirmation.id);
                    },
                    confirmedEmailConfPage: () async {
                      await Navigator.pushNamed(context, ChatScreen.id);
                    }
                    ).registerNewUser();

This is the class object that receives the Navigator.pushNamed but doesn't want to allow it to work and send users to the correct screen.

class Registration {
  Registration(
      {required this.awaitingEmailConfPage,
      required this.confirmedEmailConfPage});
  final _auth = FirebaseAuth.instance;
  Future Function() awaitingEmailConfPage;
  Future Function() confirmedEmailConfPage;

  void registerNewUser() async {
    try {
      User? user = _auth.currentUser;
      if (user!.emailVerified) {
        await awaitingEmailConfPage;
      } else {
        await confirmedEmailConfPage;
      }
    } 
  }

CodePudding user response:

If you want to call a function that receive as parameter you need to use call() method:

if (user!.emailVerified) {
        await awaitingEmailConfPage.call();
      } else {
        await confirmedEmailConfPage.call();
      }
  • Related