I am writing a todo app with flutter. i have a timepicker and datepicker. i wanted to write a function that when I press the custom button, add the values to the database, instead of when i press ok in date or time picker. but I don't know what to give for context because i get context can't be null. here is the function and some variables.
BuildContext contextBuild;
createTask () async {
var date = selectDate(contextBuild);
DatabaseHelper dbHelper = DatabaseHelper();
Task newTask = Task(date: date.toString());
await dbHelper.insertTask(newTask);
}
and this is the timepicker function
selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
builder: (context, child) {
return Theme(
data: ThemeData.light(),
child: child,
);
},
context: context,
initialDate: selectedDate, // Refer step 1
firstDate: DateTime(2020),
lastDate: DateTime(2025),
);
if (picked != null){
var formated = DateFormat.yMMMd().format(picked);
return formated;
} else if (picked == null) {
var nullFormatted = DateFormat.yMMMd().format(selectedDate);
return nullFormatted;
}
}
For example:
// in class declaration
DateTime _selectedDate;
// In build method
FlatButton(
child: Text('Select date'),
onPressed: () async {
_selctedDate = await selectDate(context);
},
),
FlatButton(
child: Text('Save'),
onPressed: () async {
// Save data in database w/o context
_saveInDatabase();
},
),