[英]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-safety
和Nullable
类型的引入,您需要仔细检查包提供的参数。
The onDidReceiveLocalNotification
doesn't guarantee that the title
, body
and payload
would not be null. onDidReceiveLocalNotification
不保证title
、 body
和payload
不会是 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.