Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter getx Rxn value, how to update null value?

Tags:

flutter

dart

I don't know English very well. So I use Google Translation. Sorry!

my flutter project use getx package

[store code]

class TestStore extends GetxController {
  static TestStore get to => Get.find<TestStore >();
  Rxn<double> test = Rxn<double>();

  void aFuntion() async {
    test(1); // screen update
    // test value = 1
  }

  void bFunction() {
    test(null); // no screen update 
    // test value = null
  }
}

[Test Screen code]

class ABFuntionTest extends StatelessWidget {
  const ABFuntionTest({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.put(TestStore());
    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: [
            TextButton(onPressed: TestStore.to.aFunction, child: Text("A function button")),
            TextButton(onPressed: TestStore.to.bFunction, child: Text("B function button")),
            Obx(
              () => Text("${LoginStore.to.test.value}")
            ),
          ],
        ),
      ),
    );
  }
}

why bFunction code no screen update ? how to null value update and screen update?

like image 789
덕상상 Avatar asked Feb 01 '26 21:02

덕상상


2 Answers

I found a solution.

Rxn<double> test= Rxn<double>();

    void bFunction() {
    test(test.value = null);
    // or test.value = null;
    }

thank you

like image 122
덕상상 Avatar answered Feb 04 '26 09:02

덕상상


Rxn is not nullable, use Rx if you want null value

or use 0 instead null for double

void aFuntion() async {
    test.value = 1; // screen update
  }

void bFunction() {
    test.value = 0;
  }
like image 42
anggadaz Avatar answered Feb 04 '26 09:02

anggadaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!