Flutter GetX: Navigating to New Product Details Page with Different Product ID Fails to Load New Data

Question

I am developing an e-commerce app using Flutter with GetX for state management. I have a ProductDetailsView that displays product details using a named route and arguments. In the ProductDetailsController, I load product details based on the product ID passed through arguments.

Here is my controller code:

import 'package:e_commerce/api/send_request.dart';
import 'package:e_commerce/app/data/color_collections.dart';
import 'package:e_commerce/app/widgets/theme_mode.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class ProductDetailsController extends GetxController {
  WebViewController? webViewController;
  var webViewHeight = 100.0.obs;
  var productDetailsLoading = true.obs;
  var productDetails = <String, dynamic>{}.obs;
  var productId = 0.obs;

  @override
  void onInit() {
    super.onInit();
    productId.value = Get.arguments['product_id'];
    loadPage();
  }

  @override
  void onClose() {
    super.onClose();
    productId.close();
  }

  void loadPage() async {
    productDetailsLoading.value = true;
    var resp = await SendRequest("/Products/Manager/CRUD/GetProductDetails", data: {
      "product_id": productId.value,
    });

    productDetails.value = resp["product_details"];
    productDetailsLoading.value = false;

    if (webViewController == null) {
      webViewController = WebViewController()
        ..setJavaScriptMode(JavaScriptMode.unrestricted)
        ..setBackgroundColor(isDarkMode()
            ? DynamicColorsCollection.backgroundColorDark
            : DynamicColorsCollection.backgroundColorLight)
        ..setNavigationDelegate(
          NavigationDelegate(
            onProgress: (int progress) {},
            onPageStarted: (String url) {},
            onPageFinished: (String url) async {
              final result = await webViewController?.runJavaScriptReturningResult(
                  "document.documentElement.scrollHeight.toString();");
              String heightStr = result as String;
              double height = double.parse(heightStr.replaceAll('"', ''));
              updateHeight(height);
            },
            onHttpError: (HttpResponseError error) {},
            onWebResourceError: (WebResourceError error) {},
            onNavigationRequest: (NavigationRequest request) {
              if (request.url.startsWith('https://www.youtube.com/')) {
                return NavigationDecision.navigate;
              }
              return NavigationDecision.prevent;
            },
          ),
        )
        ..loadRequest(Uri.parse(
            "http://some-domain.com/products/description/${resp["product_details"]["product_url"]}"));
    } else {
      webViewController?.loadRequest(Uri.parse(
          "http://some-domain.com/products/description/${resp["product_details"]["product_url"]}"));
    }
  }

  void updateHeight(double height) {
    webViewHeight.value = height;
  }
}

Here is the start of my view code:

class ProductDetailsView extends GetView<ProductDetailsController> {
  const ProductDetailsView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    Get.create<ProductDetailsController>(() => ProductDetailsController());
    return Obx(() {
      if (controller.productDetailsLoading.value) {
        return Material(
          color: isDarkMode()
              ? DynamicColorsCollection.backgroundColorSecondaryDark
              : DynamicColorsCollection.backgroundColorSecondaryLight,
          child: LoadingShimmer(),
        );
      }
      return CustomScaffold(
        backgroundColor: isDarkMode()
            ? DynamicColorsCollection.backgroundColorDark
            : DynamicColorsCollection.backgroundColorLight,

The issue arises when I navigate to a new product details page from the current product details page using a button. The button’s code is as follows:

Get.toNamed(
  Routes.PRODUCT_DETAILS,
  arguments: {
    "product_id": id,
  },
  preventDuplicates: false,
);

When I navigate like this, the new product details page does not load the new product data. Instead, it shows the data of the previous product.

How can I ensure that navigating to a new product details page with a different product ID loads the new product’s data correctly?

I tried navigating to the new product details page from the current product details page by using the Get.toNamed method with the new product ID as an argument. I expected the new product details page to load and display the details of the new product corresponding to the new product ID. However, instead of loading the new product’s data, the page continued to display the data of the previously viewed product. I expected the ProductDetailsController to fetch and show the details of the new product based on the updated product ID passed in the arguments, but this did not happen.

Try This Getx Controller:

var data = Get.arguments;

@override
void onInit() {
 productId.value = data["product_id"].toString();
 super.onInit();
 loadPage();
}

New contributor

Kishan Kacha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật