我要监听 firebaseAuthStateChanges
流并提供流以streamProvider
根据流值更改视图。我这样做了:
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
Stream get currentUser => _auth.authStateChanges();
}
final userStream = StreamProvider.autoDispose((ref) => AuthService().currentUser);
class AuthenticationWrapper extends ConsumerWidget {
@override
Widget build(BuildContext context, ScopedReader watch) {
final user = watch(userStream);
print('AutenticationWrapper build method got called');
return user.when(
data: (data) {
if (data?.uid == null) {
print('I am currently Logged out ');
return LogInPage();
} else {
print('I am logged in user');
return HomePage();
}
},
loading: () => CircularProgressIndicator(),
error: (e, s) => Text('Oops'),
);
}
}
AsyncValue
我期望在更改时呈现登录页面streamProvider
。上面的代码没有按预期工作;实际上,它会打印消息,但不会返回它应该返回的 Widget。但是,当我热重启应用程序时,它将根据流值呈现正确的 Widget。
为什么 build 方法在 this:final user = watch(userStream);
收到更新时不重新渲染?