0

I have 2 screens,

  • Screen one contains a list view with onPressed action on every item
  • screen two contains the detail of the pressed item as well as a drawer with the same list view as screen one.

What I want to do here is when the user goes to the detail screen and click on an item from the drawer the detail screen should pop and push back with new params.

Code so far,

Route

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

Binding

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

Click action in screen one

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

Detail Screen Class

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

Click action in detail screen sidebar

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

First Get.back() is to close the drawer, then remove the route and push the same route back again,

Expected behaviour,

MarketDetailController should be deleted from memory and placed again,

What actually happening

The controller only got delete and not getting back in memoery on drawer click action until I hot restart the app(By clicking save).

If anybody understands it, please help I am stuck here.

Muhammad Usman
  • 2,419
  • 1
  • 11
  • 18

1 Answers1

2

As I can see, you're trying to pop and push the same route with a different parameter in order to update a certain element on that route. Well, if that's the case then just let me show you a much better way.

In your MarketDetailController class you should add those:

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;
  }
}

Now inside your page UI, you can wrap the widget that will display the market details with the Obx widget like this:

Obx(
  () {
    final Market currentMarket = controller.currentMarket.value;

    // now you have the market details you need
    // use it and return your widget

    return MyAwesomeMarketDetailsWidget();
  },
)

Now for your click action, it can just be like this:

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

This should be it. Also, I advise you to change GetView to GetWidget and Get.lazyPut() to Get.put()

Michael Amir
  • 215
  • 1
  • 4
  • 17