【发布时间】:2020-10-07 14:35:03
【问题描述】:
我在颤振中向我的集团添加新事件时出错。 第一次没问题,但第二次出现此错误:
The following NoSuchMethodError was thrown while handling a gesture:
The method 'call' was called on null.
Receiver: null
Tried calling: call()
我将LoginEvent 添加到 bloc 中,在这种情况下我产生 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();
}
}
【问题讨论】: