简体   繁体   English

如何铸造未来<null>到未来<dynamic>在 Flutter 中使用flutter_local_notifications?</dynamic></null>

[英]How to cast Future<Null> to Future<dynamic> in Flutter while using flutter_local_notifications?

I was working with flutter_local_notifications plugin in flutter and while initializing it I got this error.我正在使用 flutter 中的 flutter_local_notifications 插件,在初始化它时出现此错误。

The argument type 'Future<Null> Function(int, String, String, String)' can't be assigned to the parameter type 'Future<dynamic> Function(int, String?, String?, String?)?'

The code I am using is:我正在使用的代码是:

void main() async {
  SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
  WidgetsFlutterBinding.ensureInitialized();

  WidgetsFlutterBinding.ensureInitialized();

  var initializationSettingsAndroid = AndroidInitializationSettings('logo');
  var initializationSettingsIOS = IOSInitializationSettings(
      requestAlertPermission: true,
      requestBadgePermission: true,
      requestSoundPermission: true,
      onDidReceiveLocalNotification:
          (int id, String title, String body, String payload) async {}),//Error on this line
          
        
  var initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: (String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
  });
  runApp(MyApp());
}

Is there a way to cast Future to Future in this function>?有没有办法在这个函数中将 Future 转换为 Future>?
Help will be really Appreciated.帮助将不胜感激。 Thanks!谢谢!

Seems like you are using the null safe version of that package.好像您正在使用 package 的null safe版本。

With the introduction of null-safety and Nullable types you need to carefully check what parameters that the packages are providing.随着null-safetyNullable类型的引入,您需要仔细检查包提供的参数。

The onDidReceiveLocalNotification doesn't guarantee that the title , body and payload would not be null. onDidReceiveLocalNotification不保证titlebodypayload不会是 null。 That is why the definition is as such inside their code,这就是为什么定义在他们的代码中,

typedef DidReceiveLocalNotificationCallback 
  = Future<dynamic> Function(int id, String? title, String? body, String? payload);

Notice the ?注意? symbol which signifies that they are Nullable types, so you should define your callback in the same fashion.表示它们是Nullable类型的符号,因此您应该以相同的方式定义回调。

Change your code to this,将您的代码更改为此,

onDidReceiveLocalNotification:
      (int id, String? title, String? body, String? payload) async {})

Got a Solution,Its regarding the NULL Safety in flutter得到了解决方案,它关于 flutter 中的 NULL 安全性
Change the error line to this:将错误行更改为:

onDidReceiveLocalNotification:
          (int id?, String title?, String body?, String payload?) async {}),//Error on this line
      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 flutter_local_notifications 安排通知 - Schedule notification using flutter_local_notifications 如何使用flutter插件显示多个通知:flutter_local_notifications - How to display multiple notifications using flutter plugin : flutter_local_notifications flutter_local_notifications onNotificationReceived 方法 - Flutter - flutter_local_notifications onNotificationReceived method - Flutter 计划通知未显示(Flutter_local_notifications) - scheduled notifications not showing (Flutter_local_notifications) 在 Flutter 中,我们如何使用 Firebase Messaging onBackgroundMessage 创建通知,使用 flutter_local_notifications? - In Flutter, how do we use Firebase Messaging onBackgroundMessage to create a notification, using flutter_local_notifications? 有没有办法使用 flutter_local_notifications 跳过重复通知? - Is there a way to skip a recurring notification using flutter_local_notifications? 如何将flutter_local_notifications添加到我的应用程序? - How do I add flutter_local_notifications to my app? 如何解决 flutter_local_notifications PlatformException 错误? - How to solve flutter_local_notifications PlatformException error? 在 flutter_local_notifications 上禁用振动 - Disable vibration on flutter_local_notifications 在 Xcode 中找不到模块“flutter_local_notifications” - Module 'flutter_local_notifications' not found in Xcode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM