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

Cannot set observable.value #788

Open
vandadnp opened this issue Apr 6, 2022 · 4 comments
Open

Cannot set observable.value #788

vandadnp opened this issue Apr 6, 2022 · 4 comments
Labels
question Further information is requested

Comments

@vandadnp
Copy link
Contributor

vandadnp commented Apr 6, 2022

Hi and thank you for MobX, it's amazing, I appreciate the hard work.

Given the following code:

void testIt() {
  final greetings = Observable('Hello');
  final dispose = autorun((_) {
    print(greetings.value);
  });
  greetings.value = 'World';
  dispose();
}

I am getting the following error in Flutter:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building HomePage(dirty):
Side effects like changing state are not allowed at this point. Please wrap the code in an "action". Tried to modify: Observable@1
'package:mobx[/src/core/context.dart]()':
package:mobx/…/core/context.dart:1
Failed assertion: line 220 pos 18: '_state.isWithinBatch'

It seems like we are not allowed to set the value of an observable like that anymore but the examples all show the code above as being valid.

@vandadnp vandadnp changed the title Cannot access observable.value within autorun Cannot set observable.value Apr 6, 2022
@amondnet amondnet added the question Further information is requested label Apr 6, 2022
@amondnet
Copy link
Collaborator

amondnet commented Apr 6, 2022

@vandadnp

Please wrap the code in an "action".

By default, MobX enforces a rule that all mutations to observables should happen inside an action. This is part of the configuration for a ReactiveContext. If you mutate an observable that is being observed, outside of an action, MobX will throw an Exception. This is a safety measure to ensure there are no stray mutations happening in your application.

Do not mutate observables outside an action.

Although not advisable, you can relax this restriction by setting a different config for the ReactiveContext.

docs: https://mobx.netlify.app/api/action/#safety-first

void testIt() {
  final greetings = Observable('Hello');
  final dispose = autorun((_) {
    print(greetings.value);
  });
  runInAction((){
      greetings.value = 'World';
  });
  dispose();
}

@vandadnp
Copy link
Contributor Author

vandadnp commented Apr 7, 2022

Thank you @amondnet , much appreciated

My issue is that the code I pasted is mentioned in the documentations, in the Core Concepts section, and shown as being valid code. Please see the attached screenshot:

image

I believe we need to update the documentations. Thoughts?

@pavanpodila
Copy link
Member

Absolutely...would love a PR from you @vandadnp if you can :-)

@vandadnp
Copy link
Contributor Author

vandadnp commented Apr 8, 2022

Sure, I'll take a look 💙

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

No branches or pull requests

3 participants