【问题标题】:Issue implementing StateNotifierProvider from Riverpod从 Riverpod 实现 StateNotifierProvider 的问题
【发布时间】:2022-02-10 05:27:03
【问题描述】:

我正在尝试使用 Riverpod 实现一个购物车系统来管理购物车的状态。

这是用户点击产品以将其添加到购物车的部分:

   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);
                            
                          }




                      },
                    )

这里有提供者类:

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

final cartListProvider = StateNotifierProvider((ref){

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

});

我还在小部件树的顶部添加了 ProviderScope。

我的问题是我在执行时遇到了错误:

cartInstance = context.read(cartListProvider)

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

总是在read(...) 部分

编辑器显示提示The method 'read' isn't defined for the type 'BuildContext'.

【问题讨论】:

标签: flutter dart riverpod


【解决方案1】:

从 Riverpod 1.0.0 中删除 context.read 以支持 ref.read

所以用ref.read替换context.read

试试这个:

    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);
            }
          },
        );
      },
    );

查看here 了解更多信息。

【讨论】:

    猜你喜欢
    • 2022-08-14
    • 2023-01-25
    • 2021-03-24
    • 2022-10-25
    • 2021-04-29
    • 2021-12-28
    • 2022-06-11
    • 2021-01-28
    • 1970-01-01
    相关资源
    最近更新 更多