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

Error when using fireImmediately with ReactionBuilder #1030

Open
ciprig opened this issue Dec 6, 2024 · 0 comments
Open

Error when using fireImmediately with ReactionBuilder #1030

ciprig opened this issue Dec 6, 2024 · 0 comments

Comments

@ciprig
Copy link

ciprig commented Dec 6, 2024

If I use ReactionBuilder with a fireImmediately reaction or autorun flutter throws the following error:

Error: dependOnInheritedWidgetOfExactType<_ScaffoldMessengerScope>() or dependOnInheritedElement() was called before ReactionBuilderState.initState() completed.
When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.

This is because the inherited widget is accessed using the context from ReactionBuilder before the initState of ReactionBuilder has finished.

This can be reproduced in the example app by changing connectivity_widgets.dart the reaction to fireImmediately

child: ReactionBuilder(
            builder: (context) {
              return reaction((_) => store.connectivityStream.value, (result) {
                final messenger = ScaffoldMessenger.of(context);

                messenger.showSnackBar(SnackBar(
                    content: Text(result == ConnectivityResult.none
                        ? 'You\'re offline'
                        : 'You\'re online')));
              }, fireImmediately: true, delay: 4000);
            },
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

1 participant