Return repeated in action on the flutter login screen

Asked

Viewed 28 times

-1

  bool _login;
  Future<bool> login(String email, String senha) async {
    var response = await http.get("http://192.168.0.116:5001/usuarios/login/$email/$senha");
    if(response.statusCode == 200) {
      return _login = true;
    } else {
      return _login = false;
    } 
  }

Knob:

    RaisedButton(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      color: HotelAppTheme.buildLightTheme().primaryColor,
      onPressed: () {
        logar();
        _login == true ? print('s') : print('n');
 
      },
      child: Text('Entrar', style: TextStyle(
        fontSize: SizeConfig.of(context).dynamicScaleSize(size: 18),
        color: Colors.white
        ),
      ),
    );

If I enter a valid email and password and click on the button, I have as return "s", so far everything working as expected, however, if I change and put an invalid email or password, and click the button again, the return is still "s", but if I click the button again, the return changes to "n" as expected. Why in the first action it does not update?

I thank anyone who can help me.

1 answer

0


Make the function onPressed also asynchronous button, and wait for completion of login function:

[...]
onPressed: () async {
                    await logar();
                    _login == true ? print('s') : print('n');
             
                  }
[...]

How did you not use the await, the function onPressed will not wait for the return of get, so using the variable value _login wrong.

For more learning about asynchronous calls and the keyword await, try it this guide (in English), or search for other questions here on the same network.

  • Thank you very much!

  • You are welcome! If the answer pleased you, do not forget to mark it as accepted to help any searches on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.