如何将 Riverpod Todos 应用程序的 StateNotifierProvider 转换为对来自 Firestore 的数据使用 StreamProvider?

时间:2021-02-22 12:19:33

标签: list flutter dart stream riverpod

我一直在研究 Remi 提供的这个 https://technerds.com/contact-us/ 作为使用 Riverpod 的示例。

我的问题是,如何将代码的 StateNotifierProvider 转换为使用 StreamProvider 而不是虚拟 Json?

如果你查看 main.dart 文件,你会发现这段代码。

final todoListProvider = StateNotifierProvider((ref) {
  return TodoList([
    Todo(id: 'todo-0', description: 'hi'),
    Todo(id: 'todo-1', description: 'hello'),
    Todo(id: 'todo-2', description: 'bonjour'),
  ]);
});

因此,我希望从 Firestore 中获取它,而不是这些虚拟数据。我曾尝试在 StateNotifier 中执行此操作。 但它不起作用,所以我认为 StateNotifierProvider 是一个更好的地方。

class CartRiverpod extends StateNotifier<List<CartItemModel>> {

    CartRiverpod([List<CartItemModel> products]) : super(products ?? []);
        final productStream = FutureProvider<List<ProductModel>>((ref) async {
        final ListofProducts = await ref.watch(productListStreamProvider);
 });
        final productStream = ref.watch(productListStreamProvider);

            void add(ProductModel addProduct) {
            bool productExists = false;
            state = [

                        ...state, new CartItemModel(product: addProduct),
      ];
    }

是否可以在 StateNotifier 中执行此操作?如果没有,我该如何修改我的代码以在 StateNotifierProvider 中执行此操作?

0 个答案:

没有答案