我正在创建一个按钮,当按下该按钮时,它将切换到另一个视图,我正在使用3个不同的文件,一个包含所有功能的root_page.dart和2个dart文件(doctor_list.dart和doctor_map。 dart),其中包含2个不同的视图。但是我按下按钮,我收到此错误消息“在null上调用了'call'方法。接收者:null尝试调用:call()”。我该如何解决?
root_page.dart
class RootPage extends StatefulWidget {
@override
_RootPageState createState() => _RootPageState();
}
enum Views {
doctorList,
doctorMap
}
class _RootPageState extends State<RootPage> {
Views _currentView;
void initState() {
super.initState();
setState(() {
_currentView = Views.doctorMap;
});
}
void _toDoctorListView() {
setState(() {
_currentView = Views.doctorList;
});
}
void _toDoctorMapView() {
setState(() {
_currentView = Views.doctorMap;
});
}
@override
Widget build(BuildContext context) {
if(_currentView == Views.doctorList) {
return DoctorListView(
toDoctorMapView: _toDoctorMapView,
);
} else if (_currentView == Views.doctorMap) {
return DoctorMapView(
toDoctorListView: _toDoctorListView,
);
} else {
return DoctorMapView(
toDoctorListView: _toDoctorListView,
);
}
}
}
doctor_map.dart
class DoctorMapView extends StatefulWidget {
DoctorMapView({this.toDoctorListView});
final VoidCallback toDoctorListView;
@override
_DoctorMapViewState createState() => _DoctorMapViewState();
}
class _DoctorMapViewState extends State<DoctorMapView> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Constants.APPBAR_COLOR,
title: Text("Hello"),
),
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
onPressed: () {
Navigator.pop(context);
},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.swap_horiz),
onPressed: () {
widget.toDoctorListView();
},
)
],
),
),
);
}
}
doctor_list.dart
class DoctorListView extends StatefulWidget {
DoctorListView({this.toDoctorMapView});
final VoidCallback toDoctorMapView;
@override
_DoctorListViewState createState() => _DoctorListViewState();
}
class _DoctorListViewState extends State<DoctorListView> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Constants.APPBAR_COLOR,
elevation: 1.0,
title: Text(
"Pediatricians",
style: Constants.APPBAR_TITLE,
),
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
size: 22.5,
color: Constants.LIGHT_COLOR,
),
onPressed: () {
Navigator.pop(context);
},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.swap_horiz),
onPressed: () {
widget.toDoctorMapView();
},
)
],
),
body: ChatListContainer(),
),
);
}
}