Flutter时间选择器错误:不要在异步间隙中使用'BuildContext',尝试重写代码以不引用“BuildContext”

erhoui1w  于 8个月前  发布在  Flutter
关注(0)|答案(1)|浏览(72)

我在我的flutter应用程序中收到了来自时间选择器的错误消息:

不要在两个空格之间使用'BuildContext'。尝试重写代码,不引用'BuildContext'。

下面是我正在使用的代码:

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);
}
}

我尝试添加**if(!安装)返回;**错误不会消失。有人知道我能做些什么来解决它吗?

a0x5cqrl

a0x5cqrl1#

要摆脱警告,您可能需要将代码 Package 在if block

if(context.mounted){

timeOfDay = await showDateTimePicker()
/// code continuation 

}

相关问题