Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter GETX – What is the difference between variable.value = newValue and variable(newValue)?

I’ve always been thinking that in Flutter GetX library, if I have an observable variable defined in this way

Rxn<MyClass> myObsObject = Rxn<MyClass>();

I could update its value by doing:
myObsObject(newValue) / myObsObject(null)
or in an EQUIVALENT WAY
myObsObject.value = newValue / myObsObject.value = null

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Today I discovered that the 2 methods are NOT equivalent, since doing the first way in some cases (don’t know how to reproduce ATM) doesn’t retrigger the builder of a GetX widget, while the second does.

So what is/are the difference(s) between the two methods up above?

>Solution :

You can check the implementation of the first way and you will see that it is this:

T call([T? v]) {
  if (v != null) {
    value = v;
  }
  return value;
}

So basically, it still does the assignment to value except when the value is null.

So myObsObject(newValue) should work fine in all cases except when newValue is null

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading