0

我正在为我的应用程序使用本地通知,我正在尝试设置onSelectNotification但我无法使其工作。我就是这样init

 Future initialize() async {
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();

    AndroidInitializationSettings androidInitializationSettings =
        AndroidInitializationSettings('app_icon');

    IOSInitializationSettings iosInitializationSettings =
        IOSInitializationSettings();

    final InitializationSettings initializationSettings =
        InitializationSettings(
            android: androidInitializationSettings,
            iOS: iosInitializationSettings);

    await flutterLocalNotificationsPlugin.initialize(
      initializationSettings, onSelectNotification: onSelectNotification
    );
  }

我的onSelectNotification

  Future onSelectNotification(String payload) async {
    print(payload);
  }

但是 Flutter 在抱怨:

参数类型“Future Function(String)”不能分配给参数类型“Future Function(String?)?”。

我在文档中找不到任何关于此的内容,在其他任何地方都找不到。我想念她什么?

4

2 回答 2

0

它应该是

Future<void> onSelectNotification(String? payload) async {...}

代替

Future<void> onSelectNotification(String payload) async {...}

它缺少? 在你的String payload

于 2021-10-22T18:09:57.290 回答
-1

Use onSelectNotification to Future < dynamic > instead.

Future<dynamic> onSelectNotification(payload) async {
   // implement logic
   print("onSelectNotification : " + payload);
}
于 2021-09-12T09:29:40.567 回答