[英]What is the RiverPod way to Consume a StreamProvider outside of Flutter - Dart only
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。
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();
});
accountStreamProvider
outside of Flutter in Dart only context?accountStreamProvider
? Again, my goal is to simply evaluate whether the stream is empty or if it contains a document.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.