Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Getx: initial value of obs variable set to null

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?

like image 405
BigPP Avatar asked Jun 25 '21 05:06

BigPP


People also ask

How do you assign a null value in Flutter?

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 } .

What is OBS in GetX Flutter?

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).

Is GetX good for Flutter?

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.


1 Answers

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>();
like image 53
S. M. JAHANGIR Avatar answered Oct 19 '22 11:10

S. M. JAHANGIR