得到了NoSuchMethodError(NoSuchMethodError:在flutter中产生新状态后,在空错误上调用了方法'call'

时间:2020-10-07 14:35:03

标签: flutter bloc

在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();
  }
}

1 个答案:

答案 0 :(得分:0)

您的代码中有2个错误:

  1. 您应该始终在try catch中编写bloc-event-state代码,这样在bloc收到某些错误后,代码将变得更加健壮
  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()
      }
    }
  )