简体   繁体   English

在 Flutter - Dart 之外使用 StreamProvider 的 RiverPod 方式是什么?

[英]What is the RiverPod way to Consume a StreamProvider outside of Flutter - Dart only

Working with RiverPod for first time and the Provider concept in general.第一次使用 RiverPod 和一般的 Provider 概念。

Goal: Validate the signed in user has an account info document in my accounts FireStore collection .目标:验证登录用户在我的帐户FireStore集合中有一个帐户信息文档

My code is based off this flutter/firebase starter architecture referenced on the RiverPod site as an example of how to use RiverPod for state management.我的代码基于RiverPod站点上引用的 Flutter /firebase 启动器架构,作为如何使用 RiverPod 进行 state 管理的示例。

I'm using latest flutter_riverpod package.我正在使用最新的flutter_riverpod package。

My minimized code:我的最小化代码:

firestore_database.dart - implementing model/service layers away from ui. firestore_database.dart - 实现远离 ui 的模型/服务层。

Stream<Account> accountStream() => _service.documentStream(
        path: FirestorePath.account(uid),
        builder: (data, documentId) => Account.fromMap(data, uid),
      );

account_setup_service.dart account_setup_service.dart

final accountStreamProvider = StreamProvider.autoDispose((ref) {
  final database = ref.watch(databaseProvider);
  return database != null ? database.accountStream() : const Stream.empty();
});

This is the part I'm stuck on.这是我坚持的部分。 Everything else checks out.其他一切都检查出来。 How do I consume the accountStreamProvider outside of Flutter in Dart only context?如何仅在 Dart 上下文中使用 Flutter 之外的accountStreamProvider Again, my goal is to simply evaluate whether the stream is empty or if it contains a document.同样,我的目标是简单地评估 stream 是否为空或是否包含文档。

Consumer(builder: (context, watch, _) {
      final accountAsyncValue = watch(accountStreamProvider);

      return accountAsyncValue.when(
        // what do I do here to validate that there is an account 
        // info document for the currently authorized user 
        // using the accountStreamProvider?
        // I don't want to return a widget...
        data: (account) => null,
        loading: () => null,
        error: (_, __) => null,
      );
    });

On the RiverPod doc site I've read the " Reading a provider outside of providers using Dart only " reference, however, I'm not sure where to go with it to consume the stream and evaluate data if there is any.在 RiverPod 文档站点上,我阅读了“ 仅使用 Dart 读取提供程序之外的提供程序”参考,但是,我不确定 go 在哪里使用 ZF7B44CFAFD5C52223D5498E 和评估数据是否存在。

The documentation link you posted shows how to get a StateController from a ProviderContainer .您发布的文档链接显示了如何从ProviderContainer获取StateController

It looks like the StateController class has a stream property, so you could listen to that:看起来StateController class 有一个stream属性,所以你可以听一下:

https://pub.dev/documentation/riverpod/latest/riverpod/StateController-class.html https://pub.dev/documentation/riverpod/latest/riverpod/StateController-class.html

(I haven't tried that, but that's what I can make out from the docs) (我还没有尝试过,但这就是我可以从文档中得出的结论)

暂无
暂无

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

相关问题 Riverpod 的 StreamProvider 只产生一次 StreamValue | Flutter &amp; Hive - Riverpod's StreamProvider yields StreamValue only once | Flutter & Hive 如何在 Flutter 中使用 Riverpod 关闭 StreamProvider - How to close StreamProvider with Riverpod in Flutter 将参数传递给 Flutter Riverpod StreamProvider - Pass parameters to Flutter Riverpod StreamProvider Flutter Riverpod:使用 StreamProvider 返回 2 stream - Flutter Riverpod : return 2 stream with StreamProvider Riverpod StreamProvider,有没有办法在 StreamProvider 本身中读取先前的值? - Riverpod StreamProvider, is there a way to read the previous value, within the StreamProvider itself? Riverpod:是否有在另一个 StreamProvider 中读取 StreamProvider 的正确方法? - Riverpod: Is there a correct way to read a StreamProvider within another StreamProvider? 使用 Flutter Riverpod 的 StreamProvider 从 Firestore 获取特定文档 - Using Flutter Riverpod's StreamProvider to fetch a specific document from Firestore Flutter Riverpod StreamProvider 在构建 Widget 之前不等待循环完成 - Flutter Riverpod StreamProvider not waiting for for loop to finish before building Widget Riverpod 的 StreamProvider 在读取 Hive 的盒子时卡在加载中 | Flutter - Riverpod's StreamProvider stuck in loading when reading Hive's box | Flutter 如何在 Flutter(Dart)的新路线中获取儿童的 StreamProvider 数据 - How to get StreamProvider data in children with new routes in Flutter (Dart)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM