why i get so much reads in firebasestore? flutter chat app

Viewed 91

i have a problem, i just created my chat app it was working perfectly until i tried to add some features.

my app takes so much read from fire base store. it takes over 1000 read per minuets.

i tried to figure out where is the problem and i found it, it was in my stream builder, but the problem i don't know how to fix it maybe my logic was wrong.

here is the stream builder code.

class Streambuild extends StatelessWidget {
  final String roomid;
  Streambuild({@required this.roomid});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
        stream: _store
            .collection('FCHATROOM')
            .doc(roomid)
            .collection("CHAT")
            .orderBy('time')
            .snapshots(),
        builder: (context, snap) {
          if (!snap.hasData) {
            return Center(
              child: Text('No Messages'),
            );
          }
          return Expanded(
            child: ListView.builder(
                itemCount: snap.data.docs.length,
                reverse: true,
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  final messgaes = snap.data.docs.reversed;
                  List<String> text = [];
                  List<String> sender = [];
                  List<String> time = [];
                  for (var msg in messgaes) {
                    text.add(msg.data()['message']);

                    sender.add(msg.data()['sender']);

                    time.add(msg.data()['realtime']);
                  }
                  return Bubblemsg(
                    txt: text[index],
                    sender: sender[index],
                    realtime: time[index],
                    isMe: (_auth.currentUser.email == sender[index]),
                  );
                }),
          );
        });
  }
}

here is bubblemsg class.

class Bubblemsg extends StatelessWidget {
  Bubblemsg(
      {this.sender, this.txt, this.isMe, this.realtime});
  final String txt;
  final String sender;
  final bool isMe;
  final String realtime;

  @override
  Widget build(BuildContext context) {
    var myprov = Provider.of<Help>(context, listen: false);
    return Container(
      width: double.infinity,
      child: Bubble(
        elevation: 5,
        padding: BubbleEdges.all(10),
        margin: isMe
            ? BubbleEdges.only(top: 10, right: 5, left: 30)
            : BubbleEdges.only(top: 10, left: 5, right: 30),
        alignment: isMe ? Alignment.topRight : Alignment.topLeft,
        nip: isMe ? BubbleNip.rightTop : BubbleNip.leftTop,
        color: isMe ? myprov.mid : Colors.grey[900],
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            txt != null
                ? txt.startsWith("https://firebasestorage.googleapis.com/v0")
                    ? Imagecontainer(
                        url: txt,
                      )
                    : SelectableText(
                        txt != null ? txt : "",
                        style: TextStyle(
                          color: myprov.word,
                          fontSize: 20,
                        ),
                      )
                : Text(""),
            Padding(
              padding: EdgeInsets.only(top: 10.0),
              child: Text(
                realtime != null ? realtime : "",
                style: TextStyle(fontSize: 15, color: myprov.word),
                textAlign: TextAlign.end,
              ),
            )
          ],
        ),
      ),
    );
  }
}

this is my reads after a few minutes it took 12k reads

enter image description here

i am sure that my way of coding is wrong, sorry i am new at flutter.

any idea what is the problem ? thank you in advanced.

1 Answers

if you use for loop in stream builder its will increase your read so for avoiding to this issue not use for loop because you using list view builder already and stream builder also act like that loop. hope my suggestion will solve your problem.

Related