Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue implementing StateNotifierProvider from Riverpod

I am trying to implement a Cart System using Riverpod to manage the state of the Cart.

This is the part where the user clicks on a product to add it to the cart:

   GestureDetector(
                      child: Icon(Icons.shopping_cart),
                      onTap: () async {
                        print("cart test");
                        //create new cart item
                        Cart cartItem = new Cart(
                          id: product.id,
                          name: product.name,
                          oldPrice: product.oldPrice,
                          price: product.price,
                          imageUrl: product.imageUrl,
                          category: product.category,
                          quantity: 1
                        );
                        var cartInstance = context.read(cartListProvider);
                        if(isExistsInCart(cartInstance.state,cartItem))
                          context.read(cartListProvider).edit(cartItem,1);
                        else
                          {
                            context.read(cartListProvider).add(cartItem);//if already available in cart, just increase quantity
                            var string = json.encode(context.read(cartListProvider).state);
                            await storage.write(key: cartKey, value: string);
                            
                          }




                      },
                    )

Here you have the provider class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:cart_system_riverpod/models/cart.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final cartListProvider = StateNotifierProvider((ref){

  return CartList([]); //init empty cart

});

I have also added ProviderScope to the top of the Widgets tree.

My issue is that I am getting errors implementing:

cartInstance = context.read(cartListProvider)

and

context.read(cartListProvider).edit(cartItem,1);

always at the read(...) part

The editor shows the hint The method 'read' isn't defined for the type 'BuildContext'.

>Solution :

From Riverpod 1.0.0 context.read was removed in favour of ref.read

So replace context.read with ref.read

Try this:

    Consumer(
      builder: (context, ref, _) {
        return GestureDetector(
          child: Icon(Icons.shopping_cart),
          onTap: () async {
            print("cart test");
            //create new cart item
            Cart cartItem = new Cart(
                id: product.id,
                name: product.name,
                oldPrice: product.oldPrice,
                price: product.price,
                imageUrl: product.imageUrl,
                category: product.category,
                quantity: 1);
            var cartInstance = ref.read(cartListProvider);
            if (isExistsInCart(cartInstance.state, cartItem))
              ref.read(cartListProvider).edit(cartItem, 1);
            else {
              ref.read(cartListProvider).add(
                  cartItem); //if already available in cart, just increase quantity
              var string = json.encode(ref.read(cartListProvider).state);
              await storage.write(key: cartKey, value: string);
            }
          },
        );
      },
    );

Check here to read more.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading