【发布时间】:2020-10-27 16:49:32
【问题描述】:
我对使用 Firebase Auth 实现 Provider 有点困惑。每个教程似乎都使用不同的方法。
我有一个名为 AuthService 的服务,其中包含一个变量 final FirebaseAuth auth = FirebaseAuth.instance。所以,我想使用下面的stream:AuthService.auth.onAuthStateChanged。问题是我应该使用类似的东西:
StreamProvider<FirebaseUser>.value(
value: AuthService().auth.onAuthStateChanged,
child: ...
或者像这样创建一个构造函数:
Provider<AuthService>(
create: (_) => AuthService(),
child: ...
)
并在小部件的某处访问它:
final authService = Provider.of<AuthService>(context);<br>
final user = authService.auth.onAuthStateChanged;
【问题讨论】:
-
我可以知道你到底想通过
Auth实现什么吗? -
我要获取当前用户。如果用户未登录,
onAuthStateChanged返回null。 -
请检查我的答案,你不需要提供者
标签: flutter dart flutter-provider