Flutter:Firebase身份验证和Firestore快照流 [英] Flutter: Firebase auth and Firestore snapshot stream

查看:65
本文介绍了Flutter:Firebase身份验证和Firestore快照流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter应用中,我正在使用Firebase进行身份验证和Firestore数据库。我也在尝试遵循BLOC模式。

In my flutter app I'm using firebase for the authentication and firestore database. I am also trying to follow the BLOC pattern.

成功登录后,我需要使用存储在文档中的一些数据来构建UI屏幕:

After succesfully login I need to build my UI screen using some data stored in a document:

 @override
  Widget build(BuildContext context) {

    return FutureBuilder(
      future: Auth.of(context).firebase.uid(), 
      builder: (context, snap){

        if(snap.hasData) {
          print("Your uid is ${snap.data}");
          return StreamBuilder(

            stream: Firestore.instance.collection('USERS').document(snap.data).collection('PHOTOS').document(
                widget.photoID).snapshots(),
            builder: (context, AsyncSnapshot<DocumentSnapshot> snap) {...}

现在,一切正常。

Auth是Firebase Auth的包装类,扩展了Inherited Widget的.uid()方法。返回带有用户标识符的Future
然后我使用StreamBuilder来构建U我通过uid来获取DocumentSnapshot。

Auth is a wrapper class for Firebase Auth extending InheritedWidget where .uid() method returns a Future with the user identifier. Then I use a StreamBuilder to build the UI passing the uid to get a DocumentSnapshot.

所以这是我的问题:是否有更好的方法来实现这一目标,并避免将StreamBuilder嵌套在FutureBuilder中?

So here is my question: is there a better approach to achieve this and avoid nesting StreamBuilder inside FutureBuilder?

推荐答案

RxDart 是您所需要的,可以将 Future Stream 组合在一起,然后再将它们传递给 StreamBuilder

RxDart is what you need, you can combine the Future and Stream before you pass them into a StreamBuilder.

  var mergedStream = Auth.of(context).firebase.uid().asStream().flatMap((uid) =>
      Firestore.instance
          .collection('USERS')
          .document(uid)
          .collection('PHOTOS')
          .document(widget.photoID)
          .snapshots());

然后您可以在 StreamBuilder

这篇关于Flutter:Firebase身份验证和Firestore快照流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆