View Mobx Information Snackbar

Asked

Viewed 54 times

0

I want to display a Snackbar with login error, but I did not find information about it and the way I tried it not working... the login itself works normal, authenticated and all, but I can not pass the error message

This code is part of my login screen, where I get the Mobx-managed authentication status in the API

@override
  void didChangeDependencies() {
    super.didChangeDependencies();

    disposer = reaction((_) => loginState.logado, (logado) {
      if (logado = true)
        Navigator.of(context)
            .pushReplacement(MaterialPageRoute(builder: (_) => Dashboard()));
      else if (logado = false)
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text(
            "Não foi possivel realizar o seu login.",
          ),
        ));
    });
  }

This is my login button, also getting information from mobx

                        Observer(builder: (_) {
                          return ElevatedButton(
                            onPressed: loginState.apertouLogin,
                            child: loginState.carregando
                                ? SizedBox(
                                    height: altura / 40,
                                    width: largura / 20,
                                    child: CircularProgressIndicator(
                                      strokeWidth: largura / 200,
                                      valueColor: AlwaysStoppedAnimation(
                                        Color(0xFFFFFFFF),
                                      ),
                                    ),
                                  )
                                : Text(
                                    "LOGIN",
                                    style: TextStyle(),
                                  ),
                          );
                        }),

1 answer

1


The code itself is not wrong, it was just a "lack" of information...

After hitting my head a lot and not understanding where I was going wrong (and without any answer), I decided to do a test, so I did + 1 @observable in my login status management file with the name of "code", so it was like this:

 @observable
  int codigo = 0;

After that, I went and added in one of my login’s @action a new value for the "code" and I did it for each reaction when logging in, that is, each of them returned me a different value, so with that, I could pull on my interface and ready and in fact it was just like that

So on the interface, instead of me pulling the @observable "logged in" which was a bool, I pulled the "code" which was an int and it worked out, as I want no matter how many times a person tries to log in, it will always return an error (so I switched the Reaction to the autorun) or log in, so my code looks like this:

 disposer = autorun((_) {
      //executa infinitamente, mas executa ao iniciar o app
      if (dadosLogin.codigo == 1) {
        Navigator.of(context).pushReplacement(MaterialPageRoute(
          builder: (_) => Dashboard(),
        ));
        dadosLogin.codigo = 0;
      } else if (dadosLogin.codigo == 2) {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text(
            "2.",
          ),
        ));
        dadosLogin.codigo = 0;
      } else if (dadosLogin.codigo == 3) {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text(
            "3",
          ),
        ));
        dadosLogin.codigo = 0;
      } else {}
    });

I don’t know if it’s the best way, but it was the way I was able to display a Snackbar without using + 1 package

I found that if I do not change the code after each result, it will run "endlessly", then with each answer, I added "dataLogin.codigo = 0;" to end the cycle

Browser other questions tagged

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