3

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

WatsMyName
  • 4,240
  • 5
  • 42
  • 73

2 Answers2

8

You can set preventDuplicates to false:

Get.to(() => const ProductDetails(), 
    arguments: [{"Details": item}], 
    preventDuplicates: false
);
0

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.

  • 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 page – WatsMyName Nov 04 '22 at 03:13
  • 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](/help/how-to-answer). – Community Nov 07 '22 at 20:51