execute method

  1. @override
Future execute(
  1. BuildContext context,
  2. ScopeManager scopeManager, {
  3. DataContext? dataContext,
})
override

TODO: each Action does all the execution in here use DataContext to eval properties. ScopeManager should be refactored so it contains the update data context (its DataContext might not have the latest data)

Implementation

@override
Future execute(BuildContext context, ScopeManager scopeManager,
    {DataContext? dataContext}) async {
  String? storageKey =
      Utils.optionalString(scopeManager.dataContext.eval(key));
  String? errorReason;

  if (storageKey != null) {
    try {
      dynamic value = await StorageManager().readSecurely(storageKey);
      // dispatch onComplete with the retrieved value
      if (onComplete != null && value != null) {
        ScreenController().executeAction(context, onComplete!,
            event: EnsembleEvent(null, data: value));
      } else if (onComplete != null && value == null) {
        // Key exists but value is null
        errorReason = 'No value found for key: $storageKey';
      }
    } catch (e) {
      errorReason = e.toString();
    }
  } else {
    errorReason = '${ActionType.readKeychain} requires a key.';
  }

  if (onError != null && errorReason != null) {
    ScreenController().executeAction(context, onError!,
        event: EnsembleEvent(null, error: errorReason));
  }
  return Future.value(null);
}