我目前正在开发一个应用程序,该程序需要从用户那里获取日期时间并进行存储。 我正在使用一个将datetime从子代传递到父代的回调,但始终收到以下错误。
在DateTimePicker类中,onPressed执行callBack函数,它将日期传递给其父级,这可能是我的问题所在。
我是个新手,我认为我的代码有点混乱,但是有些帮助将不胜感激。
The following NoSuchMethodError was thrown while handling a gesture:
The method 'call' was called on null.
Receiver: null
Tried calling: call(Instance of 'DateTime')
子类的代码(我认为是问题所在)
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:intl/intl.dart';
class DateTimePicker extends StatelessWidget {
final Function callBack;
const DateTimePicker({Key key, this.callBack}) : super(key: key);
@override
Widget build(BuildContext context) {
String _date = "Not set";
return Container(
padding: EdgeInsets.only(left: 15, right: 15),
child: Column(
children: <Widget>[
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
elevation: 4.0,
onPressed: () {
DatePicker.showDatePicker(context,
theme: DatePickerTheme(
containerHeight: 250.0,
cancelStyle: TextStyle(
fontFamily: 'Lato', fontSize: 22, color: Colors.red),
doneStyle: TextStyle(
fontFamily: 'Lato', fontSize: 22, color: Colors.blue),
itemStyle: TextStyle(
fontFamily: 'Montserrat',
fontSize: 20,
color: Colors.black),
itemHeight: 50,
),
showTitleActions: true,
minTime: DateTime(2000, 1, 1),
maxTime: DateTime(2022, 12, 31), onConfirm: (date) {
print('confirm $date');
callBack(date);
_date = DateFormat.yMMMd().format(date);
}, currentTime: DateTime.now(), locale: LocaleType.en);
},
child: Container(
alignment: Alignment.center,
height: 65,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Icon(
Icons.calendar_today,
size: 28.0,
color: Colors.white,
),
SizedBox(
width: 20,
),
Text(
" $_date",
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontFamily: 'Lato'),
),
],
),
)
],
),
Text(
" Change",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontFamily: 'Lato'),
),
],
),
),
color: Color.fromRGBO(79, 67, 67, 1),
)
],
),
);
}
}
父类的代码
import 'package:flutter/material.dart';
import '../widgets/date_picker.dart';
import 'package:provider/provider.dart';
import '../providers/event_provider.dart';
class EventPicker extends StatefulWidget {
@override
_EventPickerState createState() => _EventPickerState();
}
class _EventPickerState extends State<EventPicker> {
String eventName;
String dateTime;
void callBack(DateTime dateFromChild) {
this.dateTime = dateFromChild.toIso8601String();
}
@override
Widget build(BuildContext context) {
final add = Provider.of<EventProviders>(context);
return SingleChildScrollView(
child: Center(
child: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 40, bottom: 60.0),
child: Text(
'Add Events',
style: Theme.of(context).textTheme.headline,
),
),
DateTimePicker(),
SizedBox(height: 27),
Theme(
data: ThemeData(
primaryColor: Colors.grey,
primaryColorDark: Colors.red,
fontFamily: 'Lato',
),
child: Padding(
padding: const EdgeInsets.only(left: 15.0, right: 15.0),
child: TextField(
onChanged: (value) {
eventName = value;
},
autofocus: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Event',
labelStyle: TextStyle(
color: Colors.white,
fontFamily: 'Montserrat',
),
hintText: 'eg: Exam ',
hintStyle: TextStyle(color: Colors.white, fontSize: 20)
.copyWith(fontFamily: 'Lato'),
),
maxLength: 20,
style: TextStyle(color: Colors.white, fontSize: 22),
cursorColor: Colors.white,
),
),
),
SizedBox(
height: 10,
),
IconButton(
onPressed: () => add.addEvent(eventName, dateTime),
icon: Icon(
Icons.save,
color: Colors.white,
size: 30,
),
),
Text(
'Save',
style: Theme.of(context).textTheme.body2,
),
],
),
),
),
);
}
}