【问题标题】:How to use StreamBuilder in Flutter To Stream Result from other StreamBuilder如何在 Flutter 中使用 StreamBuilder 流式传输来自其他 StreamBuilder 的结果
【发布时间】:2019-10-26 12:43:01
【问题描述】:

我有两个 StreamBulder collection("Users").document("PhoneNum") 中的第一个 Stream 是内容名称和照片。 sec 集合中的流(“用户”).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

【问题讨论】:

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


    【解决方案1】:

    它是由listview.builder中使用的sec streambuilder解决的,它从第一个streambuilder返回,它给出了用户列表,sec streambuilder将根据第一个流的结果获得详细信息

    【讨论】:

      猜你喜欢
      • 2022-12-25
      • 2021-12-06
      • 2022-01-06
      • 2020-07-29
      • 2019-10-09
      • 2019-11-29
      • 2021-02-21
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多