Flutter 如何使用StateNotifierProvider中的StreamProvider?

Flutter 如何使用StateNotifierProvider中的StreamProvider?,flutter,dart,state,provider,riverpod,Flutter,Dart,State,Provider,Riverpod,我正在尝试使用StateNotifierProvider中的StreamProvider 这是我的StreamProvider,到目前为止运行良好 final productListStreamProvider = StreamProvider.autoDispose<List<ProductModel>>((ref) { CollectionReference ref = FirebaseFirestore.instance.collection('products

我正在尝试使用StateNotifierProvider中的StreamProvider

这是我的StreamProvider,到目前为止运行良好

final productListStreamProvider = StreamProvider.autoDispose<List<ProductModel>>((ref) {
  CollectionReference ref = FirebaseFirestore.instance.collection('products');
  return ref.snapshots().map((snapshot) {
    final list = snapshot.docs
        .map((document) => ProductModel.fromSnapshot(document))
        .toList();
    return list;
  });
});
这是我的CartRiverPod StateNotifier

class CartRiverpod extends StateNotifier<List<CartItemModel>> {

  CartRiverpod([List<CartItemModel> products]) : super(products ?? []);

  void add(ProductModel product) {
    state = [...state, new CartItemModel(product:product)];
    print ("added");
  }

  void remove(String id) {
    state = state.where((product) => product.id != id).toList();
  }
}
类CartRiverpod扩展StateNotifier{
CartRiverpod([列出产品]):超级(产品??[]);
无效添加(ProductModel产品){
状态=[…状态,新CartItemModel(产品:产品)];
印刷品(“添加”);
}
无效删除(字符串id){
state=state.where((product)=>product.id!=id).toList();
}
}

实现这一点的最简单方法是接受
读取器作为StateNotifier的参数

例如:

类CartRiverpod扩展StateNotifier{
CartRiverpod(此项为[列出产品]):超级(产品??[])){
//在StateNotifier中的任意位置使用_read可访问任何提供程序。
//例如_read(productListStreamProvider);
}
最终读者阅读;
无效添加(ProductModel产品){
状态=[…状态,新CartItemModel(产品:产品)];
印刷品(“添加”);
}
无效删除(字符串id){
state=state.where((product)=>product.id!=id).toList();
}
}
最终cartRiverpodProvider=StateNotifierProvider((ref)=>CartRiverpod(ref.read,[]);

Alex,非常感谢!这正是我要找的。@RahulDenmoto不客气。请将答案标记为已接受,以帮助将来的读者。很高兴你成功了!
class CartRiverpod extends StateNotifier<List<CartItemModel>> {

  CartRiverpod([List<CartItemModel> products]) : super(products ?? []);

  void add(ProductModel product) {
    state = [...state, new CartItemModel(product:product)];
    print ("added");
  }

  void remove(String id) {
    state = state.where((product) => product.id != id).toList();
  }
}