我从一个小部件传递给另一个小部件的VoidCallback,当从第二个小部件调用时,它正常运行,如果我在同一函数中的另一个语句中添加了一个“ await”,它将中断并显示“ null调用错误”
第一个小部件:
refreshLocalList() async {
Logger.log("Refrishing list");
setState(() {appointmentsList=AppointmentsController.getLocalList();});
}
.
.
.
onTap: (){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => NewAppointment(time:times9to2[index],date: date,cameraId: cameraId.toString(),refreshLocalList: refreshLocalList,showInSnackBar: showInSnackBar,)));
},
第二小部件:
class NewAppointment extends StatefulWidget {
final String time;
final String date;
final String cameraId;
final VoidCallback refreshLocalList;
final void Function(String msg) showInSnackBar;
const NewAppointment({Key key, this.time, this.date,this.cameraId,this.refreshLocalList,this.showInSnackBar}) : super(key: key);
@override
_NewAppointmentState createState() => _NewAppointmentState();
}
class _NewAppointmentState extends State<NewAppointment> {
Doctor doctor = Doctor();
_NewAppointmentState();
Widget build(BuildContext context) {
return MaterialApp(
.
.
.
child: Text("Submit",
),
onPressed: () async {
widget.refreshLocalList();
Appointment newAppointmet = Appointment(doctor_id: DoctorsController.getDoctorIdByName(doctorNameController.text),
date: widget.date,time: widget.time,description: descriptionFieldController.text,camera_id: widget.cameraId);
AppointmentsController.addToLocalList(newAppointmet);
await Queries.createAppointment(doctorNameController.text,widget.date,widget.time,descriptionFieldController.text,widget.cameraId);
Navigator.of(context).pop();
widget.showInSnackBar("Appointment created");
}),
回叫是refreshLocalList()
在两种情况下,另一个回调widget.showInSnackBar(String)
都可以正常工作。
如果我在Queries.createAppointment
之前删除“ await”,则一切正常。
错误:
> I/flutter (14062): full doctors map size 57 E/flutter (14062):
> [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception:
> NoSuchMethodError: The method 'call' was called on null. E/flutter
> (14062): Receiver: null E/flutter (14062): Tried calling: call()
await
就可以工作,因为NewAppointment
有机会弹出,并且您的第一个小部件变得可见,因此setState()
可以很好地工作。