TrialManager class
Manages free-trial periods for products locally on-device.
Trial state is persisted via SharedPreferences so it survives app
restarts. A background Timer fires every hour to detect trials that
are ending soon or have expired.
final manager = TrialManager(preferences: await SharedPreferences.getInstance());
await manager.startTrial('primekit_pro_monthly', duration: const Duration(days: 7));
if (await manager.isInTrial('primekit_pro_monthly')) {
final remaining = await manager.getRemainingTime('primekit_pro_monthly');
print('Trial ends in ${remaining?.inDays} days');
}
manager.events.listen((event) {
if (event is TrialEventEndingSoon) {
showTrialEndingBanner(event.hoursLeft);
}
});
Constructors
- TrialManager({required SharedPreferences preferences})
-
Creates a TrialManager backed by
preferences.
Properties
-
events
→ Stream<
TrialEvent> -
Broadcast stream of TrialEvents for all managed trials.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
dispose(
) → void - Cancels the periodic expiry check and closes the event stream.
-
endTrial(
String productId) → Future< void> -
Ends the trial for
productIdimmediately by clearing stored dates and emitting TrialEventEnded. -
eventsFor(
String productId) → Stream< TrialEvent> -
Returns a Stream scoped to events for a single
productId. -
getRemainingTime(
String productId) → Future< Duration?> -
Returns the remaining trial duration for
productId, ornullif no active trial exists. -
getTrialEndDate(
String productId) → Future< DateTime?> -
Returns the UTC timestamp when the trial for
productIdends, ornullif no trial has been started. -
isInTrial(
String productId) → Future< bool> -
Returns
trueifproductIdhas an active, non-expired trial. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
startTrial(
String productId, {required Duration duration}) → Future< void> -
Starts a trial for
productIdlastingduration. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited