【问题标题】:pop and push the same route back with different params in Futter (GET X)在 Flutter (GETX) 中使用不同的参数弹出并推回相同的路由
【发布时间】:2021-04-26 13:24:21
【问题描述】:

我有 2 个屏幕,

  • 第一个屏幕包含一个列表视图,每个项目都有 onPressed 操作
  • 第二屏包含按下项目的详细信息以及与第一屏具有相同列表视图的抽屉。

我在这里想要做的是,当用户进入详细信息屏幕并单击抽屉中的一个项目时,详细信息屏幕应该会弹出并使用新参数推回。

到目前为止的代码,

路线

GetPage(
  name: '/market-detail',
  page: () => MarketDetail(),
  binding: MarketDetailBinding(),
),

绑定

class MarketDetailBinding extends Bindings {
  @override
  void dependencies() {
   Get.lazyPut(() => MarketDetailController());
  }
}

第一个屏幕中的点击操作

onTap: () {
      Get.toNamed('market-detail',
          arguments: {'market': market});
    },

详细画面类

class MarketDetail extends GetView<MarketDetailController> {
  final Market market = Get.arguments['market'];
}

点击操作详情屏幕侧边栏

onTap: () {
        Get.back();
        Get.back();
        Get.toNamed('market-detail',
            arguments: {'market': market});
      },

首先Get.back()是关闭抽屉,然后移除路由并再次推回相同的路由,

预期行为

MarketDetailController 应该从内存中删除并重新放置,

实际发生的事情

在我热重启应用程序(通过点击保存)之前,控制器只有在抽屉点击操作上被删除并且没有回到内存中。

如果有人理解它,请帮助我被困在这里。

【问题讨论】:

    标签: flutter flutter-getx


    【解决方案1】:

    如我所见,您正尝试使用不同的参数弹出并推送同一路由,以更新该路由上的某个元素。好吧,如果是这样的话,那就让我告诉你一个更好的方法。

    在您的 MarketDetailController 类中,您应该添加这些:

    class MarketDetailsController extends GetxController {
      // A reactive variable that stores the
      // instance of the market you're currently
      // showing the details of.....
      Rx<Market> currentMarket;
    
      // this method will be called once a new instance
      // of this controller gets created
      // we will use it to initialize the controller
      // with the required values
      @override
      void onInit() {
        // some code here....
        // .......
    
        // intializing the variable with the default value
        currentMarket = Market().obs;
        super.onInit();
      }
    
      void updateCurrentMarket(Market market) {
        // some code here if you need....
    
        // updating the reative variable value
        // this will get detected then by the Obx widgets
        // and they will rebuild whatever depends on this variable
        currentMarket.value = market;
      }
    }
    

    现在在您的页面 UI 中,您可以使用 Obx 小部件包装将显示市场详细信息的小部件,如下所示:

    Obx(
      () {
        final Market currentMarket = controller.currentMarket.value;
    
        // now you have the market details you need
        // use it and return your widget
    
        return MyAwesomeMarketDetailsWidget();
      },
    )
    

    现在对于您的点击操作,它可以是这样的:

    onTap: () => controller.updateCurrentMarket(myNewMarketValue)
    

    应该是这样的。另外,我建议您将 GetView 更改为 GetWidget 并将 Get.lazyPut() 更改为 Get.put()

    【讨论】:

    • 我也想到了同样的解决方案。谢谢兄弟的帮助。非常感谢。
    • @MuhammadUsman 太好了,不客气,兄弟。我认为您应该将我的答案标记为解决方案,以便人们知道这个问题已经得到解答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2019-06-21
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多