2

I created a signIn page in flutter using MongoDB, when user enters valid credentials they have to be navigated to HomePage, else they should stay in the same page. How can I achieve this logic?

when they enter valid credentials I am able to print user successful in the console, but how can I navigate to next page if it is isLogin = true?

 Future<String> signIn() async {
      final response = await http.post(
        serverReceiverPath,
        headers: {'Content-Type': 'application/json'},
      );
      if(response.body == 'Login successful'){
        isLogin = true;
      }else{
        isLogin = false;
      }
      print(response.body);
      print(isLogin);
    }
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
saiveda
  • 81
  • 12

1 Answers1

4

Just use Navigator

  if(response.body == 'Login successful'){
    isLogin = true;
    Navigator.of(context).pushReplacementNamed('/home');
  }else{
    isLogin = false;
  }

You can learn more about it here: https://flutter.dev/docs/cookbook/navigation/named-routes

You can also try my ConditionalRouter implementation from this recent answer: Making Private Route in Flutter

Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86