bizz84 / firebase_auth_demo_flutter

Reference Authentication Flow with Flutter & Firebase
https://codewithandrea.com/
MIT License
674 stars 178 forks source link

User Information after signing in. #17

Closed rxxxxxxb closed 5 years ago

rxxxxxxb commented 5 years ago

Thank you for sharing this amazing repo.

Just a quick request/question.

bizz84 commented 5 years ago

The easiest way would be to wrap your HomePage with a Provider<User>, and return this in the LandingPage:

Provider<User>.value(
  value: user,
  child: HomePage(),
)

Then, in the HomePage or any descendent widgets, you can access it with Provider.of<User>(context).

urbanmania commented 5 years ago

Thansks for this great example of providers, I'm trying to get my head around this. It works fine for HomePage to get access to the user like that, but when I push a named route and try to access the user data I get Ensure the Provider is an ancestor to this Page2 Widget, what am I messing up here?

bizz84 commented 5 years ago

@urbanmania this happens because when you use Navigator.push, the new route is added as a child of the MaterialApp, and this is not a child of the LandingPage.

See the EmailPasswordSignInPage in this diagram for an example of this:

https://github.com/bizz84/firebase_auth_demo_flutter/blob/master/media/simplified-widget-tree.png

One solution is to call Provider.of<User>(context) before pushing the new route, and injecting the User as a constructor argument to the new widget.

bizz84 commented 5 years ago

@urbanmania see #39 which moves the auth StreamBuilder above the MaterialApp. This should fix any problems with Providers not found.