-
Notifications
You must be signed in to change notification settings - Fork 232
Description
What happened?
The OneSignal.User.addObserver method is part of the public API and is used to listen for changes in the user's state. The callback for this observer receives an object of type OSUserChangedState.
However, the OSUserChangedState class, which is defined in lib/src/user.dart, is not exported from the main library file lib/onesignal_flutter.dart. This makes it impossible for consumers of the package to use the addObserver method in a type-safe way without resorting to workarounds that are considered bad practice.
Steps to reproduce?
Create a new Flutter project.
Add onesignal_flutter as a dependency in pubspec.yaml.
In any Dart file, import the package: import 'package:onesignal_flutter/onesignal_flutter.dart';
Attempt to implement the user observer:
Dart
OneSignal.User.addObserver((OSUserChangedState state) {
print("OneSignal user state changed: ${state.current.onesignalId}");
});
What did you expect to happen?
The code should compile successfully, as OSUserChangedState should be a publicly available type for anyone using the addObserver method.
Actual Behavior
A compile-time error occurs:
The name 'OSUserChangedState' isn't a type, so it can't be used as a type argument.
Workarounds (and why they are not ideal)
Importing the internal file: import 'package:onesignal_flutter/src/user.dart';
This is a bad practice as it relies on the internal file structure of the package, which can change at any time without notice, breaking the consumer's code.
Using dynamic and manual casting:
Dart
OneSignal.User.addObserver((dynamic state) {
// Manual casting is required, which is not type-safe.
});
This defeats the purpose of Dart's type safety and makes the code more error-prone.
OneSignal Flutter SDK version
5.3.3
Which platform(s) are affected?
- iOS
- Android
Relevant log output
Code of Conduct
- I agree to follow this project's Code of Conduct