Flutter provider getting old category list after visit new category

71 Views Asked by At

I'm trying to display products list after click on category, problem is after click on category if I go category details page, then If I back to list page then click another category page then it first displaying old category data then new category data. My category details page looks like

class _CategoryDetailsScreenState extends State<CategoryDetailsScreen> {
     late List  _product;

    @override
    void initState() {
        //ModalRoute not work in init state without future delayed
     Future.delayed(Duration.zero).then((_) 
     {
        var _args = ModalRoute.of(context)!.settings.arguments as CategoryDetailsScreenArgs;
        Provider.of<ProductsProvider>(context,listen:false).fetchAndSetCatProducts(catid: _args.id, page: page);
     }
    }

    @override
    void dispose() {
        _product.clear();
        super.dispose();
    }

    @override
    Widget build(BuildContext context){

        _product =  Provider.of<ProductsProvider>(context).products;

        return Scaffold(
              body: _product.isEmpty? widget One : Product Widget
        )

    }

}

Problem in video : https://youtube.com/shorts/ETdz37FkA-0

I have _product!.clear() in disposed method , then why still getting old than new values ?

1

There are 1 best solutions below

0
On

I think maybe you should clear the products item inside the provider, something like

        Provider.of<ProductsProvider>(context,listen:false).clearProduct();

where you set the product in the state like a new initial product