Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter timepicker error: Don't use 'BuildContext's across async gaps. Try rewriting the code to not reference the 'BuildContext'

I am getting this error message from timepicker in my flutter app:

Don’t use ‘BuildContext’s across async gaps.
Try rewriting the code to not reference the ‘BuildContext’.

Here is the code I am using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  if (selectedDay != null) {
timeOfDay = await showTimePicker(
    context: context,
    initialTime: TimeOfDay.fromDateTime(
      now.add(
        const Duration(minutes: 1),
      ),
    ),
    builder: (BuildContext context, Widget? child) {
      return Theme(
        data: ThemeData(
          colorScheme: const ColorScheme.light(
            primary: Colors.teal,
          ),
        ),
        child: child!,
      );
    });

if (timeOfDay != null) {
  return NotificationWeekAndTime(
      dayOfTheWeek: selectedDay!, timeOfDay: timeOfDay);
}
}

enter image description here

I have tried to add if (!mounted) return; but the error doesn’t disappear. Does anyone know what I can do to solve it?

>Solution :

To get rid of the warning, you may want to wrap the code in if block

if(context.mounted){

timeOfDay = await showDateTimePicker()
/// code continuation 


}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading