防止 StreamBuilder 关闭流

时间:2021-06-04 07:17:59

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

我有两个流和一个 StreamBuilder。我想在这两个流之间切换以相应地显示数据。但问题是,当我从 Stream 1 切换到 Stream 2,然后切换回 Stream 1 时,它会显示来自 Stream 2 的数据。由此,我假设 StreamBuilder 在我切换到第二个时关闭了 Stream 1溪流。有什么办法可以防止这种情况发生吗?

代码:

final hostelStream = FirebaseFirestore.instance
      .collection("Shops")
      .where('inHostel', isEqualTo: true)
      .get()
      .asStream()
      .asBroadcastStream();

  final collegeStream = FirebaseFirestore.instance
      .collection("Shops")
      .where('inHostel', isEqualTo: false)
      .get()
      .asStream()
      .asBroadcastStream();

StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
            stream: isCollegeSelected ? collegeStream : hostelStream,
            builder: (context, snapshot) {
              if (snapshot.hasError) {
                return Center(
                    child: Text("There was an error while fetching data"));
              }
              if (snapshot.connectionState == ConnectionState.none) {
                return Container();
              }
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Container(
                    height: 90,
                    child: Center(child: CircularProgressIndicator()));
              }
              return ListView.builder(
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: snapshot.data.docs.length,
                  padding: const EdgeInsets.only(top: 0),
                  itemBuilder: (context, index) {
                    return ListTile(
                        title: Text(snapshot.data.docs[index]['title'])
                    )
                  });
            })

0 个答案:

没有答案
相关问题