Skip to content

Instantly share code, notes, and snippets.

@happyharis
Created March 14, 2020 09:23
Show Gist options
  • Save happyharis/051a43d3682d6130c262ce779092007d to your computer and use it in GitHub Desktop.
Save happyharis/051a43d3682d6130c262ce779092007d to your computer and use it in GitHub Desktop.
Proxy provider with firebase authentication
void main() {
Provider.debugCheckInvalidValueType = null;
runApp(MyAppExample());
}
class MyAppExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MultiProvider(providers: [
StreamProvider<FirebaseUser>(
create: (_) => FirebaseAuth.instance.onAuthStateChanged,
initialData: null,
),
ProxyProvider<FirebaseUser, String>(
update: (_, user, __) {
return user?.uid;
},
)
], child: AppView()),
);
}
}
class AppView extends StatelessWidget {
const AppView({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final uid = Provider.of<String>(context);
print(uid);
final isLoggedIn = uid != null;
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
SizedBox(height: 300),
Text(
'Has user signed in?',
style: Theme.of(context).textTheme.headline4,
),
SizedBox(height: 50),
AnimatedDefaultTextStyle(
child: Text(
isLoggedIn ? 'Yes' : 'No',
),
style: Theme.of(context).textTheme.headline2.copyWith(
color: isLoggedIn ? Colors.greenAccent : Colors.redAccent,
),
duration: Duration(milliseconds: 1500),
curve: Curves.ease,
),
SizedBox(
height: 100,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
FlatButton(
color: Colors.yellowAccent,
onPressed: () => FirebaseAuth.instance.signInAnonymously(),
child: Text('Sign In')),
FlatButton(
color: Colors.tealAccent,
onPressed: () => FirebaseAuth.instance.signOut(),
child: Text('Sign Out')),
],
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment