0

What I do is fetching items from Firebase then saving them in a list and then I display them in a ListView. The Problem that I have now is that I check if the list has Items and if so it displays my ListView with the Items and if there are no items it displays a Text but even but even if there are items the text gets displayed but after pressing hot reload the items get shown.

My guess was that there is a Problem with the State of the Widgets overall because stuff like SetState has no effect or a Refresh Indicator

Nimantha
  • 6,405
  • 6
  • 28
  • 69
DEFL
  • 907
  • 7
  • 20

1 Answers1

1

Is there a reason why the StreamBuilder is following your masterListStart().asStream as opposed to the masterList stream? I'm not saying it's wrong, its just not immediately obvious.

Either way, it's not rebuilding automatically because the only part of your displayStory method that is reactive to the stream doesn't get built until after the non empty list condition is met. Without implementing a reactive state management solution there's nothing in your code that automatically notifies any listeners that the state of the storysList has changed, which means nothing is triggering a rebuild. Which is why you have to hot reload to see the changes.

Without me getting too deep into your code, try returning the StreamBuilder at the top level of the displayStory method and putting all the conditionals inside its its builder method.

Loren.A
  • 4,872
  • 1
  • 10
  • 17
  • First of all thank you for the answer, regarding the thing you said about the stream builder I did it that way because I saw it like this on YouTube and didn't know there is something I could do different. And Regarding the second thing you suggested I will definitely try it out and keep you updated thanks – DEFL Feb 15 '21 at 23:43
  • I love you it works, I can't tell you how much time I had to invest because I tried using set state and then I thought about statemangemnt but statemangemnt was to complex for me etc. – DEFL Feb 16 '21 at 08:12
  • Glad it worked out. I suggest reading [this article](https://medium.com/flutter-community/flutter-firestore-you-may-be-using-it-wrong-b56fa689e489) about cleaning up your Firestore code. You have a ton of logic in your UI code and this will help clean it up. And generally, taking the time to learn a state management solution will only make things easier for you going forward. Provider or GetX is really not any more complex than learning how to use Firestore. – Loren.A Feb 16 '21 at 14:53
  • Thank you I will definitely have a look into Statemanagment because I also have a second problem with the streambuilder duplicating items when they are updated I hope state management will solve this – DEFL Feb 16 '21 at 16:16
  • I should use Statemanagment because I can tell the App which Widget changes and what to do right? – DEFL Feb 16 '21 at 16:25
  • Yep. For example with GetX you could make that `dispayStory` list into an observable `RxList`, and react to changes to its state. – Loren.A Feb 16 '21 at 16:28