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

Updating the currentUser #83

Open zakaria1159 opened 4 years ago

zakaria1159 commented 4 years ago

Hello, Thanks for the tutorials, I'm a beginner with flutter and i was following your CRUD(posts) video and trying to implement the same with the users. Updating the users works fine and I can see the users on firebase being updated (I update the fullName), however the currentUser remains the same (unless i do hot reload), can you please help me on how I can update the currentUser after editing it on the database.

Sorry if I should ask my question in a different place than here

FilledStacks commented 4 years ago

hi @zakaria1159 no this is the correct place 😁

The short answer it you have to listen to the document snapshot for the user, push an update over the user controller and then update that where it changed using notifyListeners.

The longer answer is that you should learn how firebase documents work and how they are used to provide a real time application. It uses streams, which will fire a new snapshot whenever the document in the firestore db changes. You can listen to this stream through calling .snapshots.listen on the document instead of doing a get. The reason you need to know that first is because it'll be hard to build on top of what I teach if you don't understand those concepts for firebase.

The next thing you'll need understand is how this value will be updated in the views that's displaying it. that you can see in my services video, the second scenario that I cover. After the reactive / non-reactive builds. You can use reactive services to indicate to your viewmodel that you want it to rebuild after a value has changes. You'll nee that functionality as well.

omkardeshpande3194 commented 3 years ago

I followed the pattern you suggested in provider-aechitecture-pt2

The flow is common as follows, login view gets user credentials , passes to login view model which then passes to auth service. After api call is successful auth service adds user to user stream controller and then on login success navigator.pushNamed() Which pushes to homeview

ref - https://github.com/FilledStacks/flutter-tutorials/blob/master/012-provider-architecture-pt2/2-final/lib/ui/views/home_view.dart

onModelReady: (model) => model.loadData(Provider.of(context))

The issue I am facing is that the stream controller doesn't pass the user before model.loadData() is called for homeview. After reading a few articles I got to know that there is no way to ensure if streams deliver data. Am I missing something? how can I solve this issue?

Not sure if this is the right place to ask this question