flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.62k stars 27.34k forks source link

StreamBuilder gives the initialData instead of the last stream value when it was built after stream was updated #76457

Closed Fogan1703 closed 3 years ago

Fogan1703 commented 3 years ago

This code sample represents my problem. Don't evaluate the realization, I hope it won't confuse you.

If the stream updating happened before StreamBuilder was built, StreamBuilder gives the initialData (null in my example) instead of the last value in the given stream. I think it is not expected behaviour. To see this behaivour, set the waitingTime to 3 or more.

If StreamBuilder was built before, it works normally. To see this behaivour, set the waitingTime to 1.

If I have waitingTime as 3 or more I also can do this:

await Future.delayed(Duration(seconds: 2));
yield 'Pete';
await Future.delayed(Duration(seconds: 2));
yield 'Tom';

instead of this, and Pete's name won't be shown but Tom's name will be shown.

I tried to solve this problem here, but I didn't get right for me answer. So I just rewrote it here simplier and more understandable. There I got suggestion to use bloc (or rxDart with its BehaviorSubject, as I found in other questions about it), but I think it has to be in the "native" flutter. I read answers to this question, but it didn't help.

Again, problem is that StreamBuilder gives the initialData instead of the last stream value when it was built after stream was updated

iapicca commented 3 years ago

this issue is clearly a duplicate of https://github.com/flutter/flutter/issues/75971; as @darshankawar stated in the closing post

If you disagree, please write in the comments and I will reopen it.

I recommend this issue to be closed then re-open and follow up on the previous issue to avoid reiterating the same answers and arguments

I tried to solve this problem here, but I didn't get right for me answer.

@Fogan1703 the fact that my answer doesn't fulfill your expectations doesn't make it wrong in a nutshell:

a StreamBuilder does not listen to an event emitted before it was created

I believe that this behavior is intended I invite you to provide any reference to a documentation that states otherwise

Fogan1703 commented 3 years ago

@iapicca Ok, I can't provide you reference to a documentation that states otherwise because there aren't

But will you say me to do? Completely remake my app architecture with bloc, rxdart etc? Listen to my stream in some another place and write its value to some currentData variable? It is not very good solution, although it does work.

I hope this functionality will be added in the future (some useLastDataAsInitial bool in StreamBuilder constuctor). But now I see you can't help me and I decided to make superstructure over StreamBuilder that would do what I want and upload it into the pub.dev or do pull request. I prefer this variant, so please, tell me how should I do it better and should I do it at all (I mean will it follow idealogy or should I better do some function request), if you know. Leave comment

iapicca commented 3 years ago

@Fogan1703 I think moving from a bug report to a question how to "best implement a pattern" is the right choice, but the right place for such a (valid and valuable) discussion is not flutter' main repo, but stackoverflow, discord or reddit

github-actions[bot] commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.