1

I have a class of complains in which there is a field of status. This status can be pending , inprogress, completed , rejected. In UI I have designed buttons to filter complaints on the basis of status. The issue that I am facing is that when I switch stream on button action. It still contains the data of previous stream. Can anyone help me to have 2 streams and not either of it contains data of previous stream.

    bool _isSwitched = false;

  List<Complains> complains = [];
final Stream<QuerySnapshot> _all = FirebaseFirestore.instance
      .collection('Complains').orderBy("startTime", descending: true)
      .snapshots();
  final Stream<QuerySnapshot> _pending = FirebaseFirestore.instance
      .collection('Complains').where('status', isEqualTo: 'Pending').orderBy("startTime", descending: true)
      .snapshots();
ElevatedButton(onPressed:(){
                  setState(() {
                    _isSwitched = !_isSwitched;
                    complains.clear();
                    complains2.clear();
                  });
                } , child: Text("Switch Stream"),),
StreamBuilder<QuerySnapshot>(
                    initialData: null,
                    stream: _isSwitched?_all:_pending,
                    builder: (context, snapshot) {
                      if (snapshot.hasError) {
                        Text("Error");
                      }
                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return Center(
                          child: CircularProgressIndicator(),
                        );
                      }
                      //length of stream is greater than 0
                      if (snapshot.data!.docs.length == 0) {
                        return Center(
                          child: Text("No Complains"),
                        );
                      }

                      for (var element in snapshot.data!.docs) {
                        Complains complain = Complains.fromMap(element.data() as Map<String, dynamic>);
                        _isSwitched?complains.add(complain):complains2.add(complain);
                      }
                      return ListView.builder(
                        itemBuilder: (context, index) {
                          return InkWell(
                            onTap: () {

                            },
                            child: Card(
                              margin: EdgeInsets.all(8.0),
                              elevation: 5,
                              color: Colors.white70,
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.all(Radius.circular(12.0))),
                              child: Container(
                                  padding: EdgeInsets.all(20),
                                  child:
                                  Column(children: [
                                    Text("Title\n ${complains[index].title}",style: TextStyle(fontStyle: FontStyle.italic),),
                                    Table(
                                      children: [
                                        TableRow(children: [
                                          Text("Name: " ),
                                          Text("Address: "+ complains[index].address.toString(),),
                                        ]),
                                        TableRow(children: [
                                          Text("Priority: "+ complains[index].priority.toString(),),
                                          Text("Status: "+complains[index].status.toString(),),
                                        ]),
                                        TableRow(children: [
                                          Text("Worker: "+ complains[index].worker.toString(),),
                                          Text("Service: "+ complains[index].service.toString(),),
                                        ]),
                                      ],
                                    ),
                                  ],)

                              ),

                            ),
                          );

                        },

                        itemCount: _isSwitched?complains2.length:complains.length,


                      );
                    },
                  ),
                ),
Talenel
  • 422
  • 2
  • 6
  • 25
  • Check this question I answered a few days back if it helps you https://stackoverflow.com/questions/71519847/change-a-stream-in-streambuilder-dynamically/71520174#71520174. – Roman Jaquez Mar 22 '22 at 22:01
  • Roman Jaquez I have seen your answer but it is different then what I want to achieve. Consider I have 4 stream and at button click I activate one of that at a time – Zia Ur Rehman Mar 23 '22 at 17:35
  • Oh, i thought you meant you wanted to swap them. I see - let me think about it for a sec. Thanks for your clarification. – Roman Jaquez Mar 23 '22 at 17:38
  • I was working on it today as well and found that the issue was with the List that I was using it was causing the issue not the stream but I am changing it in setState so why still it causing the issue I don't know – Zia Ur Rehman Mar 23 '22 at 17:41

0 Answers0