Closed narciero closed 5 years ago
@narciero thanks for mentioning this.
I have encountered this problem in various projects, however I haven't found a good solution yet.
One idea worth exploring is to convert the LandingPage
into a StatefulWidget
that holds a subscription to the onAuthStateChanged
stream.
This way, a local FirebaseUser
variable can be stored and updated when the user changes. And this may reduce unnecessary StreamBuilder rebuilds.
NOTE: I'm mentioning this as something worth exploring - I haven't tested it in practice.
@narciero Take a look at the latest changes: #39.
With this setup, the auth state StreamBuilder
has been moved above the MaterialApp
, and I no longer see the rebuilds. I have tested that this works when pushing a new route, as well as showing the on-screen keyboard.
Closing - but let me know if you have more questions
Another solution mentioned here is using a Stateful widget https://stackoverflow.com/questions/56835372/flutter-stream-builder-triggered-when-navigator-pop-or-push-is-called
Thanks for a great demo! I'm running into an issue trying to incorporate navigation after the user has successfully logged in.
Problem
When I call
Navigator.push(context, <any_route>)
it causes theLandingPage
widget tree to be rebuilt, which then recreates theStreamBuilder<User>
. Upon successful login, I changed your code to return anAuthenticatedLandingPage
widget instead ofHomePage
so I can load the user's profile before displaying the home page. Unfortunately, every time I navigate it reloads their profile due to the rebuilding.Question
Do you have any established best practices for dealing with loading data like this after logging in? I know the
build
method can be called at anytime it just seems like usingStreamBuilder
inside ofbuild
results in lots of recreations of theUser
stream (and any subsequent data streams dependent on theUser
like in my caseUserProfile
).Code
Thanks!