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 productId immediately 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, or null if no active trial exists.
getTrialEndDate(String productId) Future<DateTime?>
Returns the UTC timestamp when the trial for productId ends, or null if no trial has been started.
isInTrial(String productId) Future<bool>
Returns true if productId has 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 productId lasting duration.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited