Flutter Getx: initial value of obs variable set to null
Asked Answered
M

7

29

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?

Metaphysic answered 25/6, 2021 at 5:17 Comment(0)
C
81

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>();
Corded answered 25/6, 2021 at 6:49 Comment(5)
Perfect answer, thank you! One more follow-up though: could Rxn<Type> (>= Dart 2.12) affect flutter app performance?Metaphysic
Slightly. Because it needs checking unlike non-nullables (Rx<Type>()). But this shouldn't matter that much regarding your usecase.Corded
You saved me so much mateDrumbeat
Rx<LanguageData> kLanguage = Rx<LanguageData>(); I have a model class called LanguageData. I tried your solution but it gives the error 1 positional argument(s) expected, but 0 found. @S.M.JAHANGIRDottie
1 positional argument expected by 'Rx.new', but 0 found. Try adding the missing argument.Ataraxia
E
9

If you don't have an initial value for your [Rx] value (at the first time), you need to use

final Rx<YourObject?> yourObject = (null as YourObject?).obs;

Or for more organizing your code, you can create a separate class, like this

class RxNullable<T> {
  Rx<T> setNull() => (null as T).obs;
}

and use:

final Rx<YourObject?> yourObject = RxNullable<YourObject?>().setNull()
Education answered 7/1, 2022 at 11:32 Comment(0)
E
3

If anyone else does face this problem.

final Rx<YourObject?> yourObject = (null as YourObject?).obs;

will work.

but if there's any message that says "Unnecessary cast. Try removing the cast.", just add this comment

// ignore: unnecessary_cast

above the line, and then save.

Ending answered 12/1, 2022 at 16:55 Comment(0)
C
2

If you do it on class say a station class the code example is

 late Rx<Station?> station = Rx<Station?>(null);
Capitulary answered 15/11, 2023 at 7:5 Comment(0)
C
0

You can declare your observable variables like this for (dart 2.18 and above):

Rx varName = (T()).obs;

Cashman answered 1/2, 2023 at 16:46 Comment(0)
B
0

Getx in flutter, I search lot of time How class as observable

so fixed, Like this ,

  Rx<HomeModel?> mainModel =HomeModel(content: []).obs;
Beverly answered 10/2, 2023 at 15:53 Comment(0)
J
-1

Another Best way is you can make your variable optional

final Rx<YourObject>? yourObject;

then user obs when you declare your variable first time

yourObject.value = yourObject.obs;

and use it by force wrap

var newObject = yourObject!;

Jenson answered 6/7, 2023 at 5:28 Comment(1)
this way, the observed value will never be actually null. the observable will, insteadPolycrates

© 2022 - 2024 — McMap. All rights reserved.