我在MultiBlocProvider
中创建了集团,它的子级是一个BlocBuilder
,但是返回了一个事件{p>
MultiBlocListener
我得到了BlocProvider.of<PhoneAuthenticationBloc>(context).add(VerifyPhoneNumberEvent(phoneNumber: controller.text.replaceAll(' ', '')));
,而其他集团则正常工作。
您能发现BlocProvider.of() called with a context that does not contain a Bloc of type PhoneAuthenticationBloc
出什么问题吗?
PhoneAuthenticationBloc()
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<PhoneAuthenticationBloc>(
create: (context) => PhoneAuthenticationBloc(userRepository: UserRepository()),
),
// BlocProvider<AuthenticationBloc>(
// create: (context) => AuthenticationBloc(userRepository: UserRepository()),
// lazy: false,
// ),
// BlocProvider<UserBloc>(
// create: (context) => UserBloc(),
// lazy: false,
// ),
BlocProvider<BookingBloc>(
create: (context) => BookingBloc(user: widget.user),
),
BlocProvider<OrderBloc>(
create: (context) => OrderBloc(user: widget.user),
),
BlocProvider<PaymentBloc>(
create: (context) => PaymentBloc(user: widget.user),
lazy: false,
),
BlocProvider<CartBloc>(
create: (context) => CartBloc()..add(LoadCart()),
),
],
child: BlocBuilder<PaymentBloc, PaymentState>(
builder: (context, state) {
if (state is InitialStatePayment) {
return MultiBlocListener(
listeners: [
BlocListener<PhoneAuthenticationBloc, AuthenticationState>(
listener: (BuildContext context, AuthenticationState state){
...
FlatButton.icon(
color: Colors.orange,
onPressed: () {
print('pay pressed');
print(
'bookingStart is ${widget.bookingStart}, selected shop is ${widget.selectedShop}');
if (isVerified == true){
...
}
else{
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context){
return SingleChildScrollView(
child: ValidatePhoneDialog(
controller: controller,
onPressed: (){
if (controller.text.length >= 9){
Navigator.pop(context);
showDialog(
context:context,
barrierDismissible: false,
builder: (BuildContext context){
return VerifyingDialog();
}
);
BlocProvider.of<PhoneAuthenticationBloc>(context).add(VerifyPhoneNumberEvent(phoneNumber: controller.text.replaceAll(' ', '')));
} else {
scaffoldKey.currentState.showSnackBar(SnackBar(
backgroundColor: Colors.redAccent,
content: Text(
AppLocalizations.instance
.text('Wrong number'),
style: TextStyle(color: Colors.white))));
}
}
),
);
}
);
}
},
icon: Icon(
Icons.payment,
color: Colors.white,
),
label: Text(
AppLocalizations.instance.text('Pay'),
style: TextStyle(
color: Colors.white, fontSize: 20),
)),
答案 0 :(得分:1)
添加事件时,您使用了错误的上下文。 显示对话框时,小部件将放置在bloc提供程序上方的覆盖层中,因此,通过使用对话框的上下文,您无法找到bloc,因为其上方没有提供程序。
要将此名称固定为对话框上下文的其他名称(例如,dialogContext),以便在进行BlocProvider.of(context)
时,上下文是指显示对话框的窗口小部件的上下文,而不是对话框本身的上下文。 / p>