Search code examples
flutterunhandled-exception

Unhandled Exception: NoSuchMethodError: The method 'next' was called on null


I have 4 classes SignUp , Auth, PageOne and InWidget (inherited widget). In the classe signUpState i have a swiper that i can control using a controller.

SignUp

class SignUp extends StatefulWidget {
  static const String id = 'history_page';
  @override
  SignUpState createState() => SignUpState();
  goto(bool x) => createState().goto(x);
}

SignUpState

class SignUpState extends State<SignUp> {

 SwiperController _swOneCtrl;

  @override
  void initState() {
    _swOneCtrl = new SwiperController();
    super.initState();
  }

  goto(bool anim){
     _swOneCtrl.next(animation: anim);
    print("goto fired");
  }
}

Auth

class Auth extends StatelessWidget {
    SignUp s =  SignUp();
 verifyPhoneNumber() {
    s.goto(true);
  }    
 }

PageOne

class PageOneState extends State<PageOne> {
@override
  Widget build(BuildContext context) {
    final MyInheritedWidgetState state = MyInheritedWidget.of(context);
    return RaisedButton(
                color: Colors.blueGrey,
                disabledColor: Colors.grey[100],
                textColor: Colors.white,
                elevation: 0,
                onPressed: !phonebtn
                    ? null
                    : () {
                        final MyInheritedWidgetState state =
                            MyInheritedWidget.of(context);
                        state.verifyPhoneNumber();
                      },
                child: Text("CONTINUER"),
              ),
            );
}
}

The thing is i want to call verifyPhoneNumber() from auth that will call the goto() method from pageone using inwidget as intermediary but i'm getting this error :

Unhandled Exception: NoSuchMethodError: The method 'next' was called on null.

do you know why ?


Solution

  • Try to initialize at the time of declaration.

    class SignUpState extends State<SignUp> {
    
     SwiperController _swOneCtrl = new SwiperController();
    
      @override
      void initState() {
        super.initState();
      }
    
      goto(bool anim){
         _swOneCtrl.next(animation: anim);
        print("goto fired");
      }
    }
    

    Respond me if it works.