'Flutter same provider login is working fine but create account not redirecting to home page

After create account and after login in both time I'm trying to redirect user to homePage. Here, I have a Auth provider. Where I have written a function called _authenticate() , In both login and registration time I'm calling this function. In login time it's working fine but in registration time, page is not redirecting but registration data are saving in database.

My auth provider looks like :

class Auth with ChangeNotifier {

    //if isLogin true then go home page else go in login page.
    bool isLogin = false; 
    
    Future _authenticate(String email, String password, String urlSegment) async {
        
        var urlParse = Uri.parse(urlSegment);

        try {
            final response = await http.post(
                urlParse,
                headers: <String, String>{
                    'Content-Type': 'application/json',
                },
                body: jsonEncode({
                    'email': email,
                    'password': password,
                })
            );
            final responseData = jsonDecode(response.body);
            
            if(responseData['hasError'])
            {
                throw HttpException(responseData['error']);
            }else{
                _token = responseData['idToken'];
                if(_token != null)
                {
                    isLogin = true; 
                }else{
                    isLogin = false; 
                }
            }
            print(isLogin);  // in both time I'm getting true here 
            
            notifyListeners();
        } catch (error) {
            rethrow;
        }
    }

    //create user 
    Future signup(String email, String password) async {
        String url = Constants.milesSignupUrl;
        return _authenticate(email, password, url);
    }

    //login user 
    Future login(String email, String password) async {
        String url = Constants.milesLoginUrl;
        return _authenticate(email, password, url);
    }
}

In main page into consumer, I have written the below condition

home: auth.isLogin == true ? const HomePage(): const LoginPage()

From login page to signup button

TextButton(
    onPressed: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                        builder: (context) => const SignupPage()
                        ),
                    );
                },
                child: const Text('Click to create new account.'),
)

My registration button

child: ElevatedButton(  
    onPressed: () => signUpSubmit(),
    child: const Text(
        'Sign Up',
    ),
),

signUpSubmit() is looks like

Future<void> signUpSubmit() async {
        if (_formKey.currentState!.validate()) {
            try{ await Provider.of<Auth>(context, listen: false).signup(_email.text, _pass.text);}
}
}

In login time isLogin has made true , in registration time isLogin also made true. In login time it has redirect to homePage but in registration time hasn't redirecting but saved data perfectly. Where is the problem and who can I fix it ?



Sources

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

Source: Stack Overflow

Solution Source