简体   繁体   English

从 Riverpod 实施 StateNotifierProvider 的问题

[英]Issue implementing StateNotifierProvider from Riverpod

I am trying to implement a Cart System using Riverpod to manage the state of the Cart.我正在尝试使用 Riverpod 实现购物车系统来管理购物车的 state。

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:这里有提供者 class:

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.我还将 ProviderScope 添加到 Widgets 树的顶部。

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总是在read(...)部分

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

From Riverpod 1.0.0 context.read was removed in favour of ref.read从 Riverpod 1.0.0 中删除了context.read以支持ref.read

So replace context.read with ref.read所以用ref.read替换context.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.点击这里阅读更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Riverpod StateNotifierProvider 依赖于 FutureProvider - Riverpod StateNotifierProvider depend on a FutureProvider StateNotifierProvider 未在 Flutter 中保留 state(Riverpod) - StateNotifierProvider not persisting state in Flutter (Riverpod) 使用 Riverpod StateNotifierProvider 更新 Flutter UI - Flutter UI update with Riverpod StateNotifierProvider Flutter 消费者不使用 riverpod StateNotifierProvider 重建 UI - Flutter Consumer not rebuilding UI using riverpod StateNotifierProvider Flutter UI 更新失败,Riverpod StateNotifierProvider - Flutter UI update failure with Riverpod StateNotifierProvider Riverpod 测试:如何使用 StateNotifierProvider 模拟 state? - Riverpod Testing: How to mock state with StateNotifierProvider? Flutter riverpod, StateNotifierProvider 初始化 StateNotifier 最佳实践 - Flutter riverpod, StateNotifierProvider initialise StateNotifier best practice Flutter/Riverpod:UI 未在 StateNotifierProvider 中的 state 更改上更新 - Flutter/Riverpod: UI not updating on state change within a StateNotifierProvider Flutter Riverpod StateNotifierProvider:只观察模型部分的变化 - Flutter Riverpod StateNotifierProvider: only watch changes for part of a model Riverpod ref.watch(statenotifierprovider) 与 ref.watch(statenotifierprovider.notifier) - Riverpod ref.watch(statenotifierprovider) vs ref.watch(statenotifierprovider.notifier)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM