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

Issue

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

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

Answered By – Ivo Beckers

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *