我无法刷新屏幕

Posted

技术标签:

【中文标题】我无法刷新屏幕【英文标题】:I can't refresh the screen 【发布时间】:2021-06-04 06:56:15 【问题描述】:

我使用get 包,我有一个用Obx 更新的底部导航。还有一个元素搜索,在页面的顶层,一切都更新得很好,但是当我推送时,当前页面没有更新,只有当你调用 hot reload 时。有人怀疑嵌套页面没有更新是因为它超出了BottomNavigation,并且没有Obx小部件。

我的页面导航控制器:

Widget build(BuildContext context) 
    SizeConfig().init(context);
    final BottomPageController landingPageController =
        Get.put(BottomPageController(), permanent: false);
    return SafeArea(
      child: Scaffold(
        bottomNavigationBar:
            BottomNavigationMenu(landingPageController: landingPageController),
        body: Obx(
          () => IndexedStack(
            index: landingPageController.tabIndex.value,
            children: [
              ProductPage(),
              MapPage(),
              ClientPage(),
              NotePage(),
            ],
          ),
        ),
      ),
    );
  

我的页面需要更新ListView,具体取决于搜索栏中输入的值:

class Product extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return GetX<ProductController>(
      init: ProductController(),
      builder: (controller) 
        return FutureBuilder<List<ProductsCombined>>(
          future: controller.filterProduct.value,
          builder: (context, snapshot) 
            if (snapshot.hasData) 
              return Expanded(
                child: Container(
                  child: ListView.builder(
                    physics: BouncingScrollPhysics(),
                    itemCount: snapshot.data.length,
                    itemBuilder: (BuildContext context, int index) =>
                        ProductCards(
                      product: snapshot.data[index],
                    ),
                  ),
                ),
              );
             else 
              return Center(child: CircularProgressIndicator());
            
          ,
        );
      ,
    );
  

Product类中还有一个嵌套页面,访问方式如下:

onTap: () => 
          Get.toNamed(StoresScreen.routeName, arguments: companies)

Product 类立即更新,您不需要点击热重载来执行此操作,并且转换到的ProductScreen 类不能再更新,这两个类完全相同。搜索栏有效,但仅在热重载后才会过滤掉元素。

如果您需要我的代码的其他部分,例如处理ListViewcontroller,请写,我会附上。

编辑: I add a link to the video, with the screen that is not updated

【问题讨论】:

【参考方案1】:

Obx 使用起来有点棘手。我总是建议改用GetX

试试这个:

Obx(
      () => IndexedStack(
        index: Get.find<BottomPageController>().tabIndex.value,
        children: [
          ...
        ],
      ),
    ),

如果它不起作用,也可以在这种情况下使用 GetX&lt;BottomPageController&gt;。它适用于所有条件

【讨论】:

我想我写错了我的问题,在这个地方我有同样的更新工作。列表已更新,但是当我进入列表项时,它没有更新,尽管代码完全相同。 Here is the link to the video. 我认为是因为Client 屏幕已更新,因为IndexedStack 区域中包含的内容包含在Obx 中。而我进去的时候,已经不是这个IndexedStack的范围了,不过这只是猜测 到目前为止,我还无法修复它 如果你有一个像 List 这样的自定义泛型类型的列表,你应该在更新列表时使用 update(),因为 rxlist 无法检测到模型内部的变化,它只会检测到里面项目的变化列表 我通过为嵌套页面添加导航器来解决此问题

以上是关于我无法刷新屏幕的主要内容,如果未能解决你的问题,请参考以下文章