【发布时间】:2022-01-04 02:21:37
【问题描述】:
在将其迁移到 v8.0.1 之前,我正在使用适用于所有 Bloc 的 MultiBlocProvider。现在,只有第一个 Bloc (SignInBloc) 正在工作。 这是在我的 main.dart 上
return MultiBlocProvider(
providers: [
BlocProvider<SignInBloc>(
create: (context) => SignInBloc(
authenticationRepository: authenticationRepository,
userDataRepository: userDataRepository,
),
),
BlocProvider<SignUpBloc>(
create: (context) => SignUpBloc(
authenticationRepository: authenticationRepository,
userDataRepository: userDataRepository,
),
),
编辑:这是我的 SignInBloc
SignInBloc(
{required this.authenticationRepository,
required this.userDataRepository})
: super(SignInInitialState()) {
on<CheckIfSignedInEvent>(mapCheckIfSignedInEventToState);
}
Future<void> mapCheckIfSignedInEventToState(
CheckIfSignedInEvent event,
Emitter<SignInState> emit,
) async {
try {
bool isSignedIn = await authenticationRepository.checkIfSignedIn();
if (isSignedIn) {
emit(CheckIfSignedInEventCompletedState(true));
} else {
emit(CheckIfSignedInEventCompletedState(false));
}
} catch (e) {
print(e);
emit(CheckIfSignedInEventFailedState());
}
}
我不确定要显示什么,但这是我的 SignUpBloc,它与我的 SignInBloc 相似
SignUpBloc(
{required this.authenticationRepository,
required this.userDataRepository})
: super(SignUpInitialState()) {
on<SignUpWithGoogle>(mapSignUpWithGoogleEventToState);
}
Stream<SignUpState> mapSignUpWithGoogleEventToState(
SignUpWithGoogle event,
Emitter<SignUpState> emit,
) async* {
emit(SignUpInProgressState());
try {
User? checkUser = await authenticationRepository.checkIfUserExists();
if (checkUser != null) {
emit(SignUpWithGoogleInitialExistState());
} else {
bool checkDup =
await authenticationRepository.checkIfUserDup(event.name);
if (checkDup == true) {
emit(SignUpWithNameExistState());
} else {
User firebaseUser = await authenticationRepository.signUpWithGoogle();
emit(SignUpWithGoogleInitialCompletedState(firebaseUser));
}
}
} catch (e) {
print(e);
emit(SignUpWithGoogleInitialFailedState());
}
}
我的 main.dart 将调用具有 bloc 声明的启动画面
late SignInBloc signInBloc;
late SignUpBloc signupBloc;
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
signInBloc = BlocProvider.of<SignInBloc>(context);
signupBloc = BlocProvider.of<SignUpBloc>(context);
我试图做很多打印语句以检查哪个部分被调用,但我不明白为什么 SignUpBloc 不再被调用。请帮忙。谢谢!
编辑:我尝试调试。 这将触发我的 SignInBloc。我可以收听我的 SignInBloc。
signInBloc.add(CheckIfSignedInEvent());
这应该会触发我的 SignUpBloc。但它不会做任何类似于我的 SignInBloc 的事情。
signupBloc.add(SignUpWithGoogle(name: selectedName));
这是我的两个事件进行比较:
class CheckIfSignedInEvent extends SignInEvent {
@override
String toString() => 'CheckIfSignedInEvent';
}
class SignUpWithGoogle extends SignUpEvent {
final String name;
SignUpWithGoogle({required this.name});
@override
String toString() => 'SignUpWithGoogleEvent';
}
这是我在启动画面中收听状态的部分。只有 signInBloc 能够监听。
signupBloc.stream.listen((state) {
print('BLOC: signupBloc splash screen init : $state');
});
signInBloc.stream.listen((state) {
print('BLOC: signinBloc splash screen init : $state');
});
【问题讨论】:
-
SignUpBloc 不起作用是什么意思?你想什么时候调用它?如果在 initState 的最后一行设置断点,调试器会发生什么?
-
嗨,谢谢你的想法。我以前没有尝试过调试,但我现在会这样做。对于您询问的其他详细信息,我将更新我在上面添加的代码。
-
我在调试后添加了更多信息。我真的不明白为什么我的 SignUpBloc 不再工作了。另请注意,在我迁移到最新版本的 Bloc 后,这开始出现错误。
-
您是否尝试过使用上下文?像 context.read
?