【问题标题】:Unhandled Exception: NoSuchMethodError: The method 'showNotificationDaily' was called on null in flutter未处理的异常:NoSuchMethodError:方法'showNotificationDaily'在flutter中被调用为null
【发布时间】:2021-11-11 09:15:02
【问题描述】:

我正在尝试实现本地通知,但我遇到了 NoSuchMethodError,我已经调试了代码并找到了问题,但没有找到正确的解决方案。 我在 AddNotification.dart Stateful 类中创建了我这样称呼它的通知管理器类

 final NotificationManager manager;
  const AddNotification(this.manager);

然后在它的 State 类中这样调用它:

widget.manager.showNotificationDaily(1, "Asar", "isNotification", hour, minute);

在调用 AddNotification 的上一个类中,我发送了一个这样的通知管理器对象。

class AllSurah extends StatefulWidget {
  NotificationManager manager;
  @override
  _AllSurahState createState() => _AllSurahState();
}
    Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => AddNotification(widget.manager)),
                  );

我已经调试了代码,发现管理器正在从传递 NotificationManger 对象的上一个类传递 null。我该如何解决这个问题?

【问题讨论】:

标签: flutter dart flutter-local-notification


【解决方案1】:

试试下面的代码:

 NotificationManager manager = new NotificationManager();

【讨论】:

    【解决方案2】:

    问题不在于您没有将变量传递给对象。而是问题在于,当您在其上调用 showNotificationDaily 时,manager 变量为空,这会引发您看到的异常。

    您可以使用 Null Aware Operator ( ? ) 避免 Null Exceptions,如下所示:

    widget.manager?.showNotificationDaily(1, "Asar", "isNotification", hour, minute);
    

    但这只会避免Exception 被抛出。你真的应该调查一下为什么这个变量被传递为null

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2020-12-09
      • 2020-09-28
      • 2019-07-25
      • 2021-08-06
      • 2020-04-25
      • 2020-06-10
      • 2021-09-26
      • 2022-01-16
      相关资源
      最近更新 更多