我的应用中有谷歌注册功能。我的注册区有一个这样的活动
if (event is GoogleSubmitted) {
try {
final credential = await authenticationRepository.getGoogleCredential();
final user = await _firebaseAuth.signInWithCredential(credential);
if (user.additionalUserInfo.isNewUser) {
await authenticationRepository.persistUser(prefs);
yield* createUser(user);
} else {
yield* subscribeUser(user);
}
} catch (exception) {
yield* _errors(exception);
}
}
和创建用户方法:
Stream<SignUpState> createUser(UserCredential user) async* {
await PraxApiClient().createUser(email: user.user.email);
var localUser =
PraxUser(email: user.user.email, id: user.user.uid, name: user.user.displayName, photo: user.user.photoURL, isNewUser: true);
yield SignUpSuccess(localUser, true);
}
然后我的注册页面中有一个 BlocListener
:
else if (state is SignUpSuccess) {
Navigator.of(context).popUntil((route) => route.isFirst);
BlocProvider.of<AuthenticationBloc>(context).add(AuthenticationUserChanged(state.user, true));
}
布尔值 true
(保存在 SharedPreferences
中)然后传递给我的身份验证块。
class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState> {
AuthenticationBloc({
@required AuthenticationRepository authenticationRepository,
}) : assert(authenticationRepository != null),
_authenticationRepository = authenticationRepository,
super(const AuthenticationState.unknown()) {
_userSubscription = _authenticationRepository.user.listen(
(user) => add(AuthenticationUserChanged(user, user.isNewUser)),
);
}
final AuthenticationRepository _authenticationRepository;
StreamSubscription<PraxUser> _userSubscription;
@override
Stream<AuthenticationState> mapEventToState(
AuthenticationEvent event,
) async* {
if (event is AuthenticationUserChanged) {
var userRepository = UserRepository();
final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isNewUser = (prefs.getBool('is_new_user') ?? false);
然后我根据用户新注册的天气对我的路由进行排序。
if (event.user != PraxUser.empty) {
if (isNewUser) {
return AuthenticationState.signedUp(event.user);
} else {
return AuthenticationState.authenticated(event.user);
}
} else {
return AuthenticationState.unauthenticated();
}
我的问题是,一旦我进入 google 注册流程,侦听器就会从 final user = await _firebaseAuth.signInWithCredential(credential);
调用中过快地触发,并且布尔值从未保存到存储中,因此我的路由是错误的。我想知道解决这个问题的最佳方法是什么..