flutter getx Rxn 值,如何更新空值?
flutter getx Rxn value, how to update null value?
我不太懂英语。所以我用Google翻译。
抱歉!
我的flutter项目使用了getx包
[商店代码]
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
}
}
[测试屏幕代码]
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}")
),
],
),
),
);
}
}
为什么 bFunction 代码没有屏幕更新?
如何进行空值更新和屏幕更新?
Rxn 不可为空,如果需要空值请使用 Rx
或使用 0 代替 null 作为 double
void aFuntion() async {
test.value = 1; // screen update
}
void bFunction() {
test.value = 0;
}
我找到了解决方案。
Rxn<double> test= Rxn<double>();
void bFunction() {
test(test.value = null);
// or test.value = null;
}
谢谢
我不太懂英语。所以我用Google翻译。 抱歉!
我的flutter项目使用了getx包
[商店代码]
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
}
}
[测试屏幕代码]
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}")
),
],
),
),
);
}
}
为什么 bFunction 代码没有屏幕更新? 如何进行空值更新和屏幕更新?
Rxn 不可为空,如果需要空值请使用 Rx
或使用 0 代替 null 作为 double
void aFuntion() async {
test.value = 1; // screen update
}
void bFunction() {
test.value = 0;
}
我找到了解决方案。
Rxn<double> test= Rxn<double>();
void bFunction() {
test(test.value = null);
// or test.value = null;
}
谢谢