在flutter中向我的集团添加新事件时出现错误。 第一次还可以,但是第二次我遇到了这个错误:
The following NoSuchMethodError was thrown while handling a gesture:
The method 'call' was called on null.
Receiver: null
Tried calling: call()
我将LoginEvent
添加到块中,在这种情况下,我得到 LoadingState 。
第一次可以,但是第二次不能。
这是我的屏幕:
class _AuthScreenState extends State<AuthScreen> {
final AuthRepository repository = AuthRepository();
PageController controller;
@override
void initState() {
controller = PageController(initialPage: widget.page);
super.initState();
}
void changePage(int page) {
controller.animateToPage(
page,
curve: Curves.ease,
duration: Duration(milliseconds: 300),
);
}
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => AuthBloc(repository: repository),
child: BlocBuilder<AuthBloc, AuthState>(
builder: (context, state) {
return ScreenContainer(
pb: 25.0,
withTapDetector: true,
statusbarcolor: ColorPalette.primary,
statusBarIconBrightness: Brightness.light,
resizeToAvoidBottomPadding: false,
removeAppbar: true,
// loading: state is LoadingState,
child: Container(
width: double.infinity,
height: double.infinity,
child: Column(
children: [
Expanded(
child: PageView.builder(
controller: controller,
physics: NeverScrollableScrollPhysics(),
itemCount: 2,
itemBuilder: (context, position) {
return position == 0
? LoginPage(
onPageChange: () => changePage(1),
onSubmit: (req) => context.bloc<AuthBloc>().add(LoginEvent(req: req)),
)
: RegisterPage(
onPageChange: () => changePage(0),
);
},
),
),
googleLogin(context),
],
),
),
);
},
),
);
}
}
这是我的集团:
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository repository;
AuthBloc({
@required this.repository,
}) : super(InitialState());
@override
Stream<AuthState> mapEventToState(
AuthEvent event,
) async* {
if (event is RegisterEvent) {
yield* _mapRegisterEventToState(event.req);
} else if (event is LoginEvent) {
yield* _mapLoginEventToState(event.req);
}
}
Stream<AuthState> _mapRegisterEventToState(AuthReq req) async* {}
Stream<AuthState> _mapLoginEventToState(AuthReq req) async* {
yield LoadingState();
}
}
答案 0 :(得分:0)
您的代码中有2个错误:
return BlocProvider(
create: (context) => AuthBloc(repository: repository),
child: BlocBuilder<AuthBloc, AuthState>(
builder: (context, state) {
if(state is State1) {
return SocialMediaScreen()
} else if(state is State2){
return FullSpinnerScreen()
} else {
return InitialScreen()
}
}
)