pageview使用obx后,子页面怎么再使用obx监听数据?

来源:16-7 使用getx实现MVVM架构模式-模块的改造

且听风吟720

2024-05-08

如题,按照老师的课程我做了一个一样的BottomTabView,然后我想在BottomTabView里的页面继续用Obx,但是就报错
════════ Exception caught by widgets library ═══════════════════════════════════
The following message was thrown building Obx(has builder, dirty, state: ObxState#56661):
[Get] the improper use of a GetX has been detected.
You should only use GetX or Obx for the specific widget that will be updated.
If you are seeing this error, you probably did not insert any observable variables into GetX/Obx
or insert them outside the scope that GetX considers suitable for an update
(example: GetX => HeavyWidget => variableObservable).
If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.

The relevant error-causing widget was:
Obx Obx:file:///Users/shige/Desktop/AppProjects/parrot/lib/pages/card/views/card_page.dart:19:13

CardPage代码如下

class CardPage extends GetView<CardViewModel> {
  const CardPage({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('打卡'),
        ),
        body: Column(
          children: [
            const Center(child: UserInfoWidget()),
            Obx(() => CascaderWidget(
                label: '班次',
                options: controller.shiftOptions,
                onChange: (value) {}))
          ],
        ));
  }
}

这意味着pageview下的子页面不能再用Obx监听了吗?

写回答

1回答

CrazyCodeBoy

2024-05-08

使用Obx有个前提,是在当前组件中一定要有个可观察的的响应式变量才行。这个课程文档中有介绍哦。
0
1
且听风吟720
我知道问题了,因为pageview包裹,所以这个页面不需要添加额外的Obx了,直接可以监听我绑定在CardPage上的controller内部的值
2024-05-08
共1条回复

慕课甄选-Flutter零基础极速入门到进阶实战

全新Flutter从入门到进阶,实战仿携程网App

661 学习 · 316 问题

查看课程