Search code examples
flutterflutter-getx

Flutter Getx: Can obs create a global variable?


How to make obs a global variable?

Have an option of Getx that can create a global variable?

For the example:

class Test extends GetxController {
  RxString a = "".obs;
}

Page 1:

Test test =  Get.put(Test());
print(test.a.value); //""
test.a.value = "55555";
print(test.a.value); //"55555"

Page 2:

Test test =  Get.put(Test());
print(test.a.value); //""

Solution

  • Update Now, I am using

    Class + static

    Class:

    Class Global {
         static Rx<String> name = "me".obs;
         static ProfileObject profile = ProfileObject();
    }
    

    Use:

    Obx(()=>Text(Global.name.value)),
    Obx(()=>Text(Global.profile.age.value)),
    

    It is easy to use, don't setting just use static in class.