Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onDidChange called twice #76

Closed
apinprastya opened this issue Aug 28, 2018 · 4 comments
Closed

onDidChange called twice #76

apinprastya opened this issue Aug 28, 2018 · 4 comments

Comments

@apinprastya
Copy link

So I have a login screen and I need to listen if the login is success or not, if not success I just show the snackbar. I use StoreBuilder and using onDidChange to listen the state, on the first attempt it only called once, but for next login click button the onDidChange is called twice.

class _LoginPage extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  String username;
  String password;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: Text(
            "Login",
            style: TextStyle(
              color: Colors.red,
            ),
          ),
        ),
        body: Builder(
          builder: (context) {
            return StoreBuilder<AppState>(
              onDidChange: (store) {
                if (store.state.loginState.loginError) {
                  Scaffold.of(context).showSnackBar(SnackBar(
                        content: Text(store.state.loginState.loginErrorMessage),
                      ));
                }
              },

I am not sure if my approach is right, I am new at flutter and dart. Thank you for the awesome lib.

flutter_redux: 0.5.2
lutter beta 0.5.1

@brianegan
Copy link
Owner

Hey there -- thanks for letting me know! do you happen to have a bit more code you can show? The onDidChange will fire each time the Store emits an onChange event. Are you firing off two actions by chance, or in some way triggering your reducers twice as well?

@apinprastya
Copy link
Author

Hi brianegan, ofc here the full source. It still in very early of my development, i am still crafting it.
Just enter username and password, so it will fire the LoginError action. The reducer is only called once, but the onDidChange called twice. And also, it not happen always, sometime it called once, but sometimes twice (print("CALLED HERE")).
As a note, i am using ubuntu 18.04.

mdms.zip

@apinprastya
Copy link
Author

@brianegan I found another way to handle the login error and success. Instead of listening the store, I use a function callback from my action. So I put callback on my action and call the callback function when login error or login success.
I am coming from C++, so I still has a lot of C++ background when working with dart, and I realized that dart programming language is really good, the limitation is almost none.
I will close this issue btw.
Again thanks for this awesome lib.

@brianegan
Copy link
Owner

Awesome, thanks @apinprastya :) As a heads up, I managed to reproduce the issue, and I'm trying to find the root cause. It seems like the onDidChange callback is only sometimes being run twice by Flutter, but it's unclear why that's happening.

I might start scheduling this callback with scheduleMicrotask for now rather than Flutter's addPostFrameCallback method. The documentation for that method says the function should be run only once, but it seems there might be a bug in there somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants