Flutter GetX navigate to same page but with different data
Asked Answered
R

3

5

I am using GetX with my flutter project. In home page when user taps a product, it is navigated to ProductDetails like this

Get.to(() => const PropductDetails(), arguments: [
  {"Details": item}
]);

And in ProductDetails page there is list of related products, now when product is tapped, I want user navigate again to ProductDetails page but with new product details. When user taps back, he will be seeing the previously viewed Product details page.

I used same code as above in ProductDetails page

Get.to(() => const ProductDetails(), arguments: [
  {"Details": relatedItem}
]); 

Here is the minimal code of the ProductDetails view:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class ProductDetails extends StatelessWidget {
  const ProductDetails({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, //or set color with: Color(0xFF0000FF)
    ));

    return ProductDetailsBuilder(context).build();
  }
}

class ProductDetailsBuilder {
  ProductDetailsBuilder(this.context);
  final BuildContext context;

  final controller = Get.put(ProductDetailsController());

  Widget build() {
    return Scaffold(
      backgroundColor: Colors.white,
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.blue,
        elevation: 0,
        systemOverlayStyle: SystemUiOverlayStyle.light,
      ),
      // add this body tag with container and photoview widget
      body: relatedProducts(),
    );
  }

  Widget relatedProducts() {
    return Column(
      children: List.generate(controller.listRelatedProducts.length, (index) {
        var item = controller.listRelatedProducts[index];

        return Container(
          color: Colors.grey,
          width: double.infinity,
          child: ElevatedButton(
            child: Text(item.label),
            onPressed: () {
              Get.to(() => const ProductDetails(), arguments: [
                {"Details": item}
              ]);
            },
          ),
        );
      }),
    );
  }
}

But this doesn't seem to work. Can anybody please help me in this?

Thanks

Release answered 5/10, 2022 at 16:17 Comment(2)
You need to provide include the content of your ProductDetails and more information like your Getx route setting if anyCaballero
@Caballero I have added the code of ProductDetails pageRelease
J
18

You can set preventDuplicates to false:

Get.to(() => const ProductDetails(), 
    arguments: [{"Details": item}], 
    preventDuplicates: false
);
Jariah answered 11/11, 2022 at 12:59 Comment(5)
I will mark your answer as Correct. THank you!Release
It throws me an exception ``` Unhandled Exception: setState() or markNeedsBuild() called during build. ```Marcellamarcelle
While this in fact creates a non-duplicate page, the problem I am still facing is that once the second (or N page) gets removed, it also removes the controller which created that page, so any logic the controller has to do for the other non-duplicate pages, will just fail. Any way to handle that?Ecosphere
override functions => onReady, OnInit is not calledChristianism
@PabloZehle you can use tag on controller to create unique instance per screen Get.put(MyController(),tag : 'some uniq id');Homicidal
P
0

you can use

final controller = Get.create<ProductDetailsController>(() => (ProductDetailsController());

and also set preventDuplicates: false in

Get.to(() => const ProductDetails(), arguments: [
  {"Details": relatedItem}
],preventDuplicates: false);
Passkey answered 27/2 at 20:43 Comment(0)
W
-1

I also run into this, the solution is to use the default Navigator instead of getx navigation for that specific page. It will fix it.

Warford answered 3/11, 2022 at 18:37 Comment(2)
This should be a comment instead of reply. But I have fixed the issue simply by refreshing the page data instead of navigating to other instance of the pageRelease
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Enter

© 2022 - 2024 — McMap. All rights reserved.