Upon now I always use getx observable declarations like this:
var someString = ''.obs;
var someNumber = 0.obs;
and so on...
But what if some variables don't have an initial value at first, and I actually want them to be null and later change them?
When creating { question: 'How old are you?' , answer: null } try specify the precise type you want like <String, dynamic>{ question: 'How old are you?' , answer: null } .
Transforming a variable into an observable + initial value with GetX is the simplest, and most practical approach. You will literally add a " . obs " to the end of your variable, and that's it, you've made it observable, and its . value , well, will be the initial value).
GetX has a huge ecosystem, a large community, a large number of collaborators, and will be maintained as long as the Flutter exists. GetX too is capable of running with the same code on Android, iOS, Web, Mac, Linux, Windows, and on your server.
For non null-safe (pre Dart 2.12), you can declare your observable variables like this:
final someVariable = Rx<Type>();
For example:
final someString = Rx<String>();
final someNumber = Rx<int>();
And for null-safety (Dart 2.12 or later), Just use Rxn<Type>
instead of Rx<Type>
.
For example:
final someString = Rxn<String>();
final someNumber = Rxn<int>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With