更新时间:2023-09-11 21:36:10
首先,从自定义模型开始,确定您想要的东西,在这种情况下,我将简单地使用 uid
First, start with the custom model and decide what you would like to have, in this case I will simply have the uid
class User {
final String uid;
User({ this.uid });
}
// Create user object based on FirebaseUser
User _customModelForFirebaseUser(FirebaseUser user) {
return user != null ? User(uid: user.uid) : null;
}
// auth changed user stream
Stream<User> get user {
return _auth.onAuthStateChanged
.map(_customModelForFirebaseUser)
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: MaterialApp(
home: Wrapper(),
),
);
}
}
然后您可以从任何地方访问它,就像这样:
Then you can access it from anywhere, like so:
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
}