The below code working fine so far,
class Controller extends GetxController{
var count = 0.obs;
increment() => count++;
}
But when I assign a value it doesn't work as expected not event with type cast.
class Controller extends GetxController{
var count = 0.obs;
reset() => count = 10;
}
I would like to know how to assign value ( with '=' ) in get x obs objects?
This answer has been changed after I have been notified that I was wrong. I want to say thanks to Ivo Beckers to pointing put my misunderstanding of how RxInt
works. Please upvote his answer.
0.obs
returns the type RxInt
. When you call ++
on this object, it is calling the +
operator on the RxInt
object with 1
to increment the value. It should be noted that the +
and -
operators for RxInt
does manipulate the object itself and does therefore not behave like e.g. int
which is immutable and does therefore require the return of a new object from the operation.
When you do count = 10
you are actually saying you want count
to be overwritten with 10
which are of the type int
and not RxInt
.
What you want is more likely to be the following to you can report back to any subscribers that the value have been reset to 10
:
class Controller extends GetxController{
var count = 0.obs;
reset() => count.value = 10;
}