flutter - 使用 Flutter BLoc 登录 Google 失败
问题描述
上下文:我有一个带有 Google 登录选项的注册表单。
问题:如果在侦听器signIn()
内部调用,它可以工作。onPressed
但是如果我在 my 中执行调用CustomerRegistrationBloc
,它会抛出:
PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)
// Google SignIn Button
class _GoogleSigninButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<CustomerRegistrationBloc, CustomerRegistrationState>(
buildWhen: (previous, current) => previous.status != current.status,
builder: (context, state) {
return ElevatedButton(
onPressed: () async {
try {
// !!! This can work!
// GoogleSignIn _googleSigin = GoogleSignIn();
// await _googleSigin.signOut();
// var user = await _googleSigin.signIn();
// !!! This cannot work!
context
.read<CustomerRegistrationBloc>()
.add(GoogleSignUpSelected());
} catch (ex) {
print(ex);
}
},
key: const Key('customerRegistrationForm_google_signUpButton'),
child: Icon(Icons.menu, color: Colors.white),
style: ElevatedButton.styleFrom(
shape: CircleBorder(),
padding: EdgeInsets.all(14),
primary: Colors.blue,
onPrimary: Colors.red,
),
);
},
);
}
}
class CustomerRegistrationBloc
extends Bloc<CustomerRegistrationEvent, CustomerRegistrationState> {
CustomerRegistrationBloc(): super(CustomerRegistrationState());
@override
Stream<CustomerRegistrationState> mapEventToState(
CustomerRegistrationEvent event,
) async* {
if (event is GoogleSignUpSelected) {
yield* _handleSignUpWithGoogle(event, state);
}
}
Stream<CustomerRegistrationState> _handleSignUpWithGoogle(
GoogleSignUpSelected event, CustomerRegistrationState state) async* {
try {
yield state.copyWith(status: FormzStatus.submissionInProgress);
// sign in with google
GoogleSignIn _googleSigIn = GoogleSignIn();
var account = await _googleSigIn.signIn();
if (account != null) {
// get user info and populate to state
yield state.copyWith(status: FormzStatus.submissionSuccess);
}
} catch (ex) {
print(ex);
yield state.copyWith(status: FormzStatus.submissionFailure);
}
}
}
解决方案
推荐阅读
- vuejs2 - ThymLeaf 将变量传递给 vue js
- javascript - 图像不会显示在 Wordpress 模板中
- php - 可以从我的浏览器访问外部网站 Sitemap.xml,但不能使用 cURL
- maven - 配置本地 Nexus 服务器
- python - 使用 Scapy 和 Pyinstaller 的 Python CDP 客户端
- python - 如何为类似于 django 模型/字段的动态字段指定类型?
- cesium - 如何在 CZML 中设置“等待时间”?
- tsql - Tsql IF 语句未正确评估
- winforms - 在 Winforms 项目的 WindowState 最大化模式下将 50% 扩展到 50%
- javascript - 用三个js显示一个对象