2

我在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?)?'

我正在使用的代码是:

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

有没有办法在这个函数中将 Future 转换为 Future>?
帮助将不胜感激。谢谢!

4

2 回答 2

2

好像您正在使用该null safe软件包的版本。

随着null-safetyNullable类型的引入,您需要仔细检查包提供的参数。

onDidReceiveLocalNotification不保证,title和不会为空bodypayload这就是为什么定义在他们的代码中,

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

请注意?表示它们是Nullable类型的符号,因此您应该以相同的方式定义回调。

将您的代码更改为此,

onDidReceiveLocalNotification:
      (int id, String? title, String? body, String? payload) async {})
于 2021-05-30T05:45:54.650 回答
0

得到了解决方案,它关于颤振中的 NULL 安全性
将错误行更改为:

onDidReceiveLocalNotification:
          (int id?, String title?, String body?, String payload?) async {}),//Error on this line
      
于 2021-05-30T05:45:17.857 回答