名称“IOSNotificationDetails”不是一个类。尝试更正名称以匹配现有的类

Shr*_*K S 5 dart dart-pub flutter flutter-notification

我正在使用 Windows 笔记本电脑和 Android 模拟器,并且我还更新了 build.gradle 文件。我收到错误

var iOSPlatformChannelSpecifics = new IOSNotificationDetails();

在显示通知方法中。谁能帮我这个?

这是代码片段 代码片段

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';

class NotifyHelper {
   FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
     FlutterLocalNotificationsPlugin(); //

initializeNotification() async {
//tz.initializeTimeZones();
final DarwinInitializationSettings initializationSettingsDarwin =
    DarwinInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);

final AndroidInitializationSettings initializationSettingsAndroid =
    const AndroidInitializationSettings("appicon");

final InitializationSettings initializationSettings =
    InitializationSettings(
  iOS: initializationSettingsDarwin,
  android: initializationSettingsAndroid,
);
 await flutterLocalNotificationsPlugin.initialize(initializationSettings,
    onDidReceiveNotificationResponse: onDidReceiveNotificationResponse);
}

displayNotification({required String title, required String body}) async {
print("doing test");
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
    'your channel id', 'your channel name',
    importance: Importance.max, priority: Priority.high);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
// ignore: unnecessary_new
var platformChannelSpecifics = new NotificationDetails(
    android: androidPlatformChannelSpecifics,
    iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
  0,
  'You change your theme',
  'You changed your theme back !',
  platformChannelSpecifics,
  payload: 'It could be anything you pass',
);
}

void requestIOSPermissions() {
flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
        IOSFlutterLocalNotificationsPlugin>()
    ?.requestPermissions(
      alert: true,
      badge: true,
      sound: true,
    );
}

  void onDidReceiveNotificationResponse(
    NotificationResponse notificationResponse) async {
    final String? payload = notificationResponse.payload;
   if (notificationResponse.payload != null) {
  debugPrint('notification payload: $payload');
} else {
  debugPrint("Notification Done");
}
Get.to(() => Container(
      color: Colors.white,
    ));
}

/*Future selectNotification(String? payload) async {
if (payload != null) {
  print('notification payload: $payload');
} else {
  print("Notification Done");
}
Get.to(() => Container(
      color: Colors.white,
    ));
}*/

 Future onDidReceiveLocalNotification(
  int id, String? title, String? body, String? payload) async {
// display a dialog with the notification details, tap ok to go to another page
/*showDialog(
  //context: context,
  builder: (BuildContext context) => CupertinoAlertDialog(
    title: Text(title),
    content: Text(body),
    actions: [
      CupertinoDialogAction(
        isDefaultAction: true,
        child: Text('Ok'),
        onPressed: () async {
          Navigator.of(context, rootNavigator: true).pop();
          await Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => SecondScreen(payload),
            ),
          );
        },
      )
    ],
  ),
);*/
Get.dialog(const Text("Wellcome to flutter"));
}
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*tai 10

检查包的变更日志。版本10.0.0引入了一些重大更改,您所遵循的指南可能更早。

例如:

[iOS][macOS] 重大更改 iOS 和 macOS 类已被重命名和重构,因为它们基于相同的操作系统并共享相同的通知 API。它们不再具有 IOS 或 MacOS 前缀,而是被带有 Darwin 前缀的类所取代。例如,IOSInitializationSettings 可以替换为 DarwinInitializationSettings

因此,对于您当前的问题,解决方案可能是使用DarwinNotificationDetails而不是IOSNotificationDetails. 但也可能存在其他问题。