Flutter stream builders concept

306

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

stream: dataBloc.dataStream.where((item) => /* check criteria */ item['key'] == 'foo')
Share:
306
GoPro
Author by

GoPro

Look for Eijun - the ace!

Updated on November 28, 2022

Comments

  • GoPro
    GoPro over 1 year

    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
    GoPro over 5 years
    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;
  • Günter Zöchbauer
    Günter Zöchbauer over 5 years
    I see. Didn't recognize this is about Firebase. item is the same what snapshot.data returns in builder: ...
  • GoPro
    GoPro over 5 years
    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
    GoPro over 5 years
    for example, if the condition is satisfied build, if it is not satisfied build but differently.
  • Günter Zöchbauer
    Günter Zöchbauer over 5 years
    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
    Günter Zöchbauer over 5 years
    "if there is a change in the output of the where clause!" sorry, but I don't understand what that means.
  • GoPro
    GoPro over 5 years