这个错误让我伤透了脑筋。我谦虚地寻求帮助。
我不断收到以下错误。但是,在 GitHub 存储库(此处:https://github.com/OneSignal/OneSignal-Flutter-SDK/blob/main/example/lib/main.dart)上列出的示例中,.shared getter 属性是显示。我正在使用 OneSignal Fluuter 包(onesignal_flutter:^5.0.1)https://pub.dev/packages/onesignal_flutter
The getter 'shared' isn't defined for the type 'OneSignal'. Try importing the library that defines 'shared', correcting the name to the name of an existing getter, or defining a getter or field named 'shared'.
我做错了什么?
这是我试图实施的行动:
初始化一个信号
import 'package:onesignal_flutter/onesignal_flutter.dart';
Future<void> initOneSignal() async {
// Set the log level for debugging (optional)
OneSignal.shared.setLogLevel(OSLogLevel.verbose);
// Initialize OneSignal with your App ID
await OneSignal.shared.init(
"API_KEY", // Replace with your OneSignal App ID
iOSSettings: {
OSiOSSettings.autoPrompt:
false, // Set to true if you want the default iOS prompt
OSiOSSettings.inAppLaunchUrl: true,
},
);
// Request permission from the user for notifications
final status = await OneSignal.shared.getPermissionSubscriptionState();
if (status.permissionStatus.status != OSNotificationPermission.authorized) {
// Handle the case where notification permission is not authorized.
// You can show a dialog or perform any other desired action here.
// For example, you can request permission again or inform the user about the need for notifications.
}
}
提前致谢!
这是由于 5.0.0 版本所致(参见)。 GitHub 上的示例似乎是在该版本发布之前创建的。
请查看 pub.dev 上的 OneSignal 示例,它使用 5.0.0 以上的版本。
在那里你可以看到
setLogLevel
方法应该是: OneSignal.Debug.setLogLevel(OSLogLevel.verbose)
和 init
方法应该是 OneSignal.initialize