Unhandled Exception: NoSuchMethodError: The method 'next' was called on null
Unhandled Exception: NoSuchMethodError: The method 'next' was called on null
我有 4 个 类 SignUp、Auth、PageOne 和 InWidget(继承的小部件)。在类 signUpState 中,我有一个可以使用控制器控制的滑动器。
注册
class SignUp extends StatefulWidget {
static const String id = 'history_page';
@override
SignUpState createState() => SignUpState();
goto(bool x) => createState().goto(x);
}
注册状态
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");
}
}
授权
class Auth extends StatelessWidget {
SignUp s = SignUp();
verifyPhoneNumber() {
s.goto(true);
}
}
第一页
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"),
),
);
}
}
问题是我想从 auth 调用 verifyPhoneNumber(),它将使用 inwidget 作为中介从 pageone 调用 goto() 方法,但我收到此错误:
Unhandled Exception: NoSuchMethodError: The method 'next' was called on null.
你知道为什么吗?
initState()
是有状态的widget插入widget树时调用一次的方法。
如果我们需要进行某种初始化工作(例如注册侦听器),我们通常会覆盖此方法,因为与 build() 不同,此方法只调用一次。
因为我认为你在 SignUPState 中声明了 Swipe 控制器 class。
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");
}
}
但是您已经在initState()
中初始化了它。问题是因为您没有在小部件树中插入 SignUp 小部件,所以您的滑动控制器没有初始化并变为空。因此,当您将 next 方法调用为 null 时,它会显示错误。
As Solution first insert your Sign up widget in your Widget tree.
如果我的解决方案对您有所帮助。请给我打分。
尽量在声明的时候初始化
class SignUpState extends State<SignUp> {
SwiperController _swOneCtrl = new SwiperController();
@override
void initState() {
super.initState();
}
goto(bool anim){
_swOneCtrl.next(animation: anim);
print("goto fired");
}
}
如果有效请回复我。
我有 4 个 类 SignUp、Auth、PageOne 和 InWidget(继承的小部件)。在类 signUpState 中,我有一个可以使用控制器控制的滑动器。
注册
class SignUp extends StatefulWidget {
static const String id = 'history_page';
@override
SignUpState createState() => SignUpState();
goto(bool x) => createState().goto(x);
}
注册状态
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");
}
}
授权
class Auth extends StatelessWidget {
SignUp s = SignUp();
verifyPhoneNumber() {
s.goto(true);
}
}
第一页
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"),
),
);
}
}
问题是我想从 auth 调用 verifyPhoneNumber(),它将使用 inwidget 作为中介从 pageone 调用 goto() 方法,但我收到此错误:
Unhandled Exception: NoSuchMethodError: The method 'next' was called on null.
你知道为什么吗?
initState()
是有状态的widget插入widget树时调用一次的方法。
如果我们需要进行某种初始化工作(例如注册侦听器),我们通常会覆盖此方法,因为与 build() 不同,此方法只调用一次。
因为我认为你在 SignUPState 中声明了 Swipe 控制器 class。
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");
}
}
但是您已经在initState()
中初始化了它。问题是因为您没有在小部件树中插入 SignUp 小部件,所以您的滑动控制器没有初始化并变为空。因此,当您将 next 方法调用为 null 时,它会显示错误。
As Solution first insert your Sign up widget in your Widget tree.
如果我的解决方案对您有所帮助。请给我打分。
尽量在声明的时候初始化
class SignUpState extends State<SignUp> {
SwiperController _swOneCtrl = new SwiperController();
@override
void initState() {
super.initState();
}
goto(bool anim){
_swOneCtrl.next(animation: anim);
print("goto fired");
}
}
如果有效请回复我。