如何在Flutter中使用StreamBuilder从其他StreamBuilder流结果

时间:2019-06-12 04:40:40

标签: firebase flutter dart google-cloud-firestore stream-builder

我有两个StreamBulder collection(“ Users”)。document(“ PhoneNum”)中的第一流,它是内容名称和照片。 sec Collection(“ Users”)。document(“ PhoneNum”)。document(“ FrindList”)中的流 这是内容Frind PhoneNumber 我想让First Stream获得sec Stream的所有结果,而我不想为所有FrindList构建Stream到那里的名称和照片,并将其放入ListView

我尝试了很多代码,但没有用

我试图这样

StreamBuilder(
  stream: Firestore.instance.collection("Users")
      .document(names)
      .snapshots(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) {
      return CircularProgressIndicator(
        backgroundColor: Colors.white,
      );
    } else {
      List<Widget> list = [];
      var Frindname = snapshot.data["name"];
      var Frindpic = snapshot.data["ImageURL"];
      var Frindnum = snapshot.data["PhoneNumber"];
      try {

         return GestureDetector(
            onTap: () {
              var chatkey;
              if (widget.phoneN.compareTo(Frindnum) >
                  0) {
                chatkey = widget.phoneN + Frindnum;
                print(chatkey);
              } else {
                chatkey = Frindnum + widget.phoneN;
                print(chatkey);
              }

              Navigator.push(context,
                  MaterialPageRoute(
                      builder: (context) {
                        return Chat();
                      }));
            },
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.vertical(
                    bottom: Radius.elliptical(
                        100, 100),
                    top: Radius.elliptical(100, 100)),
                color: Colors.grey[200],),
              padding: EdgeInsets.all(20),
              child: Row(
                children: <Widget>[
                  CircleAvatar(
                    backgroundImage: NetworkImage(
                        Frindpic), radius: 30,),
                  SizedBox(width: 10,),
                  Text(Frindname, style: TextStyle(
                      color: Colors.deepPurple,
                      fontSize: 18,
                      fontWeight: FontWeight.bold),),
                ],
              ),
            ),

        );


      } catch (e) {
        return CircularProgressIndicator(
          backgroundColor: Colors.white,);
      }
    }
  },

);

,然后在其他streambuilder中调用此方法

StreamBuilder<QuerySnapshot>(
                stream: Firestore.instance.collection("Users").document(widget.phoneN).collection("FrindList").snapshots(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData) {
                    CircularProgressIndicator(
                      backgroundColor: Colors.white,
                    );
                  }
                    try {
                    List<Widget> e = [];
                    snapshot.data.documents.forEach((d) {
                      print(d.data["FrindKey"]);
                      e.add(Streems(d.data["FrindKey"]));
                    }

                    );
                   return ListView(
                     children: e,
                   );

                     } catch (e) {
                      print(e);
                      return Text("No Friend Found", style: TextStyle(
                          color: Colors.deepPurple,
                          fontSize: 24,
                          fontWeight: FontWeight.bold),);
                    }
                  }

                 ),

这是我的数据库结构

database structure

1 个答案:

答案 0 :(得分:0)

此问题由listview.builder中使用的sec streambuilder解决,该listview.builder从第一个streambuilder返回,并给出了用户列表,而sec streambuilder将根据第一个流的结果获取详细信息