1

I have a map in which i store key and value pair. I have put this map in a stream, which we will call dataStream for ease.

now i have a stream builder, this stream-builder uses dataStream. I want this stream builder to build only if a specific key's value is altered.

Currently my stream builder responds to any change in the map. I want to instruct it to change only when a certain key's value in the map changes. Is this possible in stream builder and how?

return StreamBuilder(
    stream: dataBloc.dataStream,
    builder: (context, snapshot) {
     if(snapshot.hasData){
     Map<String, String> dataMap = snapshot.data;
     // some logic
     }
     return widget;
     }

I want to see if a specific key's value in this map changes, only then rebuild my existing widget else, leave it as it is. Do not build it again

GoPro
  • 642
  • 1
  • 10
  • 24

1 Answers1

2

You should be able to filter for specific events using where:

stream: dataBloc.dataStream.where((item) => /* check criteria */ item['key'] == 'foo')
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Do i access the map which is inside my dataStream using snapshot.data ? I am not sure how to access it in the where clause; – GoPro Dec 07 '18 at 16:48
  • I see. Didn't recognize this is about Firebase. `item` is the same what `snapshot.data` returns in `builder: ...` – Günter Zöchbauer Dec 07 '18 at 16:51
  • I have one more question, what happens if my where clause is false; Will it rebuild? Ideally i want it to rebuild if there is a change in the output of the where clause! – GoPro Dec 07 '18 at 16:57
  • for example, if the condition is satisfied build, if it is not satisfied build but differently. – GoPro Dec 07 '18 at 16:57
  • If the where clause is false, the event will be filtered and not passed to the listener (StreamBuilder), so no, it's as if the event were never sent. – Günter Zöchbauer Dec 07 '18 at 17:24
  • "if there is a change in the output of the where clause!" sorry, but I don't understand what that means. – Günter Zöchbauer Dec 07 '18 at 17:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184893/discussion-between-gopro-and-gunter-zochbauer). – GoPro Dec 07 '18 at 17:55