gpt4 book ai didi

flutter - 如何在Firebase Phone Auth中使用Flutter Bloc

转载 作者:行者123 更新时间:2023-12-03 05:00:08 28 4
gpt4 key购买 nike

我正在尝试使用Flutter Bloc模式实现Firebase电话授权。
我有以下代码

import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:firebase_auth/firebase_auth.dart';
import './bloc.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
final FirebaseAuth _auth = FirebaseAuth.instance;

@override
AuthState get initialState => AuthNotStarted();

@override
Stream<AuthState> mapEventToState(
AuthEvent event,
) async* {
if (event is VerifyPhone) {
yield* _mapVerifyPhoneToState(event);
}
}

Stream<AuthState> _mapVerifyPhoneToState(VerifyPhone event) async* {
yield AuthStarted();
_auth.verifyPhoneNumber(
phoneNumber: "+" + event.phoneNumber,
timeout: Duration(seconds: 60),
verificationCompleted: (AuthCredential authCredential) {
print("verification completed: auth credential");
},
verificationFailed: (AuthException authException) {
print("verification failed: auth exception");
print(authException.message);
},
codeSent: (String verificationId, [int forceResendingToken]) {
print("code sent verification id" + verificationId);
},
codeAutoRetrievalTimeout: (String verificationId) {
print("auto time" + verificationId);
});
}
}

但是我不能在 verifyPhoneNumber回调中使用yield。
问题是如何在回调函数中产生不同的状态?

最佳答案

您可以从回调中添加事件。例如,在verificationCompleted中,您可以执行以下操作:

verificationCompleted: (AuthCredential authCredential) {
print("verification completed: auth credential");
add(AuthCompleted());
},

您可以处理 AuthCompleted()上的 mapEventToState事件:

@override
Stream<AuthState> mapEventToState(
AuthEvent event,
) async* {
if (event is VerifyPhone) {
yield* _mapVerifyPhoneToState(event);
}
if (event is AuthCompleted){
//Here you can use yield and whathever you want
}
}

关于flutter - 如何在Firebase Phone Auth中使用Flutter Bloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59647724/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com