I am breaking my head over this error. I am humbly reaching out for some help.
I keep receiving the error below. However, in the examples listed on the GitHub repo (here: https://github.com/OneSignal/OneSignal-Flutter-SDK/blob/main/example/lib/main.dart), the .shared getter properties are shown. I am using the OneSignal Fluuter package (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'.
What am I doing wrong?
Here is the action in which I am trying to implement:
InitOneSignal
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.
}
}
Thanks in advance!
2
Answers
This is due to the 5.0.0 release (see). It seems like the example on GitHub was created before that release.
Please take a look at the OneSignal example on pub.dev it uses a version above 5.0.0.
There you can see that the
setLogLevel
method should be:OneSignal.Debug.setLogLevel(OSLogLevel.verbose)
and theinit
method should beOneSignal.initialize
Edit: I have opened an issue on GitHub, to get the mismatch resolved
The error message "The getter ‘shared’ isn’t defined for the type ‘OneSignal’" means that the OneSignal Flutter package does not have a getter named shared. This is because the shared getter was removed in version 5.0.0 of the package.
To fix this error, you need to change your code to use the new OneSignal singleton class instead of the shared getter. For example:
The OneSignal() singleton class is the same as the shared getter, except that it does not require you to import the onesignal_flutter package. This can be useful if you are using multiple packages that depend on the OneSignal Flutter package.