FilledStacks / flutter-tutorials

The repo contains the source code for all the tutorials on the FilledStacks Youtube channel.
MIT License
4.76k stars 1.76k forks source link

045 After removing all documents it's not updating in the UI #82

Closed dsourav closed 4 years ago

dsourav commented 4 years ago

This code only can update the UI if the collection isn't empty

if (updatedMessages != null && updatedMessages.length > 0) {
        _chatMessages = updatedMessages;
        notifyListeners();
      }
FilledStacks commented 4 years ago

Then you should remove the .length chat if you want it otherwise

dsourav commented 4 years ago

Then you should remove the .length chat if you want it otherwise

It won't work until it will be listened from below

pagePostsQuery.snapshots().listen((postsSnapshot) {
      if (postsSnapshot.documents.isNotEmpty) {
        var posts = postsSnapshot.documents
            .map((snapshot) => Post.fromMap(snapshot.data, snapshot.documentID))
            .where((mappedItem) => mappedItem.title != null)
            .toList();
   }
}
FilledStacks commented 4 years ago

@dsourav I don't know if you're asking me a question or telling me how you fixed it

dsourav commented 4 years ago

@dsourav I don't know if you're asking me a question or telling me how you fixed it

I am asking you question buddy. The way you're saying isn't working. Don't you think it needs to be fixed.

FilledStacks commented 4 years ago

@dsourav in this repo no, it's an example to show how to do pagination with real time data. that's all that it should focus on. In your own code if you're using it then yes. You should fix it up. Figure out how to make it update when there's no documents and do that. i would assume updating even when the length is 0 should work fine. but I still don't understand your comment about listening above so i don't know if you tried it or not.

dsourav commented 4 years ago

@dsourav in this repo no, it's an example to show how to do pagination with real time data. that's all that it should focus on. In your own code if you're using it then yes. You should fix it up. Figure out how to make it update when there's no documents and do that. i would assume updating even when the length is 0 should work fine. but I still don't understand your comment about listening above so i don't know if you tried it or not.

I said about listening to the stream.

if (postsSnapshot.documents.isNotEmpty) 
{
...
}

You are adding value to the stream controller only when the documents aren't empty. So if there's no document then you aren't adding any value to the stream controller. Then how the length is 0 will work.

FilledStacks commented 4 years ago

Aaah, I see. Then You can just always set the value even when there's no documents.

dsourav commented 4 years ago
var pagePostsQuery = _postsCollectionReference
  .orderBy('title')
  .limit(PostsLimit)

Here pagePostsQuery contains only the paginated data. Not the entire documents in a collection. So we can't use it to check if the collection is empty.

FilledStacks commented 4 years ago

@dsourav And do we want to check if the collection is empty?

dsourav commented 4 years ago

@dsourav And do we want to check if the collection is empty?

Yes... exactly..

FilledStacks commented 4 years ago

@dsourav haha i don't know what's going on man. I'm not changing the tutorial code. The concept i was teaching is pagination with firebase realtime data. The code does that. Any other implementation details is up to the dev applying this pattern. So you'll have to check if it's empty, or not check I don't quite know what you're asking me for. It's hard to follow along with the short replies and explanations of things and I think there might be a language barrier as well.

dsourav commented 4 years ago

Real time database means it will listen changes of data whenever any changes happens to the database...so if we delete all the documents from a collection then it also needs to be listened. Since your tutorial code represent the term realtime pagination then it needs to be fixed ... otherwise you can't tell it a real time pagination.

FilledStacks commented 4 years ago

Well I definitely consider it a real-time pagination as you've seen it works. The details like removing all the documents will be up to you to implement. If I had to cover every scenario for the tutorial with my free time it would have been a 1-2 hour tutorial which would take me a lot longer than 24 hours to make.

It's up to you to work on the pattern and add your implementation details based on your need. I show high level solutions to problems, details are up to the dev to implement.

dsourav commented 4 years ago

I think it's an issue that's why a let you know about it. If you think you are okay with it and you don't want to make any changes it's totally up to you.

FilledStacks commented 4 years ago

I see. I'm happy with the way that it is. I won't be making changes to any old code tutorials. I can't even finish the new ones haha. You're always welcome to fix it and make a PR.