简体   繁体   中英

I am having problem navigating same page and the data is not chaning in flutter

How can I navigate same page like I am tapping related products in Product details page and it will navigate to Product details page according to that related product? I want to call same page over and over again.

If you make your ProductDetails page take in a Product (or id, depends on your data) you should be able to call it. It depends on how you want to build your routes (and how you want to be able to go back), of course. Say you havce your ProductDetails take in a product to display:

ProductDetails(product: myProduct)

Then it can get the related products, and you can make the related product push another ProductDetails page:

// In your product details page
Widget relatedProductsWidget() {
  return Row(
    children: relatedProducts.map((product) => ProductCard(
      product: product,
      onTap: () => Navigator.of(context).push(
        // Simply push another product on the stack
        MaterialPageRoute(builder: (context) => ProductDetails(product: product))
      ),
    )),
  );
}

It depends, of course, on how you wish to go back. This way, a whole stack of ProductDetails pages will be pushed (and popped again when the back button is pressed). Alternatively, if you only want a single product details page to be present at any time, you can use Navigator.of(context).pushReplacement instead, to animate to the new location and then dispose the old route. Now when the user goes back, he is brought back to the main product list without seeing the old product first.

https://api.flutter.dev/flutter/widgets/Navigator/push.html

https://api.flutter.dev/flutter/widgets/Navigator/pushReplacement.html

I hope this gets you on the way to get your navigation sorted. If you have a specific issue, I recommend you to post a minimal version of your code to make it clear what you're trying to accomplish, as it's still a little bit vague from your question right now.

My answer only works if you use firebase.

Just complete the steps:

  1. Create in the database a collection called products

  2. Now add the products and in all products/documents you need to have a field called id. PS: any ids have to be used only one time.

  3. Create in your code a global variable called id

  4. If you tab on one of the products the id-variable need to be set to the id of the product you tabs on

  5. In your Product-Details Page add a variable called productData

  6. Create in your Product-Details class a method that looks like this:

     getProductData() async { await FirebaseFirestore.instance.collection("products").where("id", isEqualTo: id).get().then((value) { productData = value.docs[0].data(); });}
  7. In your initState() add this method:

    asyncTasks() async { await getProductData(); }

  8. Call the method asyncTasks() in your initState

Now you can get data from the product document like this:

// Now you get the value that stands in the price field
productData["price"];

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM